Sub topic 9
async/await Under the Hood
This chapter assumes you know basic async/await syntax, how .then() callbacks work, and the microtask queue from the previous chapters. The goal is to see await for what it is: a way to split one function into pieces, with each later piece scheduled as a microtask.
The Working Model
An async function does not start out asynchronous. It runs synchronously, on the current call stack, until it reaches its first await. At that point three things happen:
- The awaited value is converted to a promise. A plain value like
nullbecomes an already-resolved promise. - The rest of the function is registered as a reaction on that promise, much like a
.then()callback. - The function returns to its caller. The first time it pauses, the caller gets back the promise that the
asyncfunction always returns.
When the awaited promise settles, the continuation is queued as a microtask. It runs once the call stack is empty and the microtask queue reaches it.
async function run() {
console.log('1');
await null;
console.log('3');
}
run();
console.log('2');
// Output: 1, 2, 3'1' logs synchronously inside run(). The await null pauses the function, and control returns to the top-level script, which logs '2'. Once the script finishes and the stack is empty, the microtask queue runs the continuation, which logs '3'.
The Promise Equivalent
The same function written with .then() looks roughly like this:
function runDesugared() {
console.log('1');
return Promise.resolve(null).then(() => {
console.log('3');
});
}
runDesugared();
console.log('2');
// Output: 1, 2, 3The engine does not literally rewrite your code like this. It suspends and resumes the function's execution context, so local variables and try/catch blocks survive the pause. The scheduling is the same, though: each await hands the rest of the function to the microtask queue.
Multiple Awaits Interleave with Other Microtasks
Each await queues a new microtask, so a function with several awaits takes turns with other promise reactions. It does not run straight through.
async function a() {
console.log('a1');
await null;
console.log('a2');
await null;
console.log('a3');
}
a();
Promise.resolve()
.then(() => console.log('p1'))
.then(() => console.log('p2'));
console.log('sync');
// Output: a1, sync, a2, p1, a3, p2After the synchronous code, the queue holds [a-continuation, p1]. Running the first continuation logs a2 and queues the next continuation behind p1. Running p1 queues p2. The two chains alternate.
Common Mistakes
| Mistake | What actually happens |
|---|---|
| Assuming code before the first `await` is asynchronous | It runs synchronously, and an expensive loop there blocks the caller |
| Thinking `await` waits for timers or I/O to "catch up" | It waits only for the awaited promise to settle, and resumes as a microtask |
| Expecting `await somePromise` to resume before other queued microtasks | It resumes in queue order, behind reactions queued before it |
| Using `return await` and assuming it costs nothing extra | Returning a promise from an `async` function can add extra microtask ticks before the caller sees the result |
Exercise
What does this code log, and in what order? async function f() { console.log('f1'); await new Promise((resolve) => setTimeout(resolve, 0)); console.log('f2'); } f(); Promise.resolve().then(() => console.log('then')); console.log('end');
Show answer
f1, end, then, f2. The first line of f runs synchronously. The awaited promise settles only when the timer task runs. Before that, the synchronous 'end' logs and the microtask queue runs 'then'. When the timer fires, it resolves the promise, which queues the continuation that logs 'f2'.