Skip to the page

async/await exactly · core

What async and await do, exactly

An async function always returns a promise and runs synchronously until its first await; await x turns x into a promise, pauses only this function (not the thread), returns control to the caller, and resumes the rest of the function in a microtask after x settles — with the value, or by throwing the rejection.

The notes

What you must know

  • async function f() {} always returns a promise. return v fulfills it with v; a throw rejects it.
  • Code before the first await runs synchronously when you call the function, like a promise executor.
  • await expr: (1) turn expr into a promise (a native promise is reused, other values are wrapped); (2) register the rest of the function as a reaction; (3) return control to the caller, whose code keeps running.
  • When the awaited promise fulfills, the rest of the function is queued as a microtask (one tick for a native promise). If it rejects, the error is thrown at the await line and try/catch can catch it.
  • await on a non-promise still waits one microtask, so the code after it runs after the current synchronous code.
  • Mental model: each await splits the function; the part after it works like a .then callback. async/await is a generator plus a promise runner.
  • Top-level await in an ES module pauses that module (and the modules that import it) until it settles.
  • await doesn't block the thread: while one function is paused, events, timers and other code keep running.

The red pen

Common traps: mistake → fix

  • Mistake: await blocks the thread

    Fix: it pauses only this function.

  • Mistake: await on a non-promise is synchronous

    Fix: it still waits one microtask.

  • Mistake: Code in an async function before the first await runs later

    Fix: it runs during the call, synchronously.

  • Mistake: async functions run on other threads

    Fix: they run on the same main thread.

Easy to hard

Questions interviewers ask, easy to hard

  1. What does an async function return?
  2. What does await do, exactly? Answer in three steps: pause this function, give control back, resume in a microtask with the value or a throw.
  3. Predict the async1/async2 ordering and explain each line.
  4. What does await null do, and why would anyone write it?
  5. How would you build async/await from generators? How many ticks does await take?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview