Promises and async/await Internals
An async function executes synchronously until its first await, then parks its Execution Context on the heap and returns a pending Promise. Once the awaited value settles, the engine schedules resumption on the Microtask Queue, never skipping a turn even for await 42.
Async Function Suspension & Microtask Resume
Trace how await suspends an execution context and resumes via the Microtask Queue.
async fn() runs synchronously on the Call Stack until it hits await.
console.log('A');
const data = await fetchJSON();- callStack
- [ "fn()" ]
- microtasks
- []
- suspended
- false
Under the Hood
V8 compiles each async function into a resumable state machine: execution runs synchronously until await, which suspends the Execution Context to the heap and schedules resumption on the Microtask Queue. Even await 1 yields for at least one microtask tick.
- Synchronous Prologue: Calling
async fn()runs straight on the Call Stack until anawaitexpression. - Context Suspension:
awaitwraps the operand inPromise.resolve(), attaches a hidden resume reaction, and pops the frame off the Call Stack. - Microtask Continuation: Even
await 1yields at least one tick to the Microtask Queue before restoring the local variables. - Zero-Cost Async Stack: V8 reconstructs async stack traces by walking promise reaction links without keeping dead stack frames alive.
// Sequential waterfall (slow) vs concurrent dispatch (fast)
async function loadDashboard(ids) {
const requests = ids.map((id) => fetch(`/api/${id}`).then((r) => r.json()));
return Promise.all(requests);
}Problem Genealogy
- 2009 (Nested Callbacks): Deeply nested error-first callbacks ("callback hell") broke
try/catchand stack traces. - 2015 (ES6 Promises): Standardized chainable state machines (
pending,fulfilled,rejected) on the Microtask Queue. - 2017 (ES2017
async/await): Compiler-desugared coroutine syntax over Promises with synchronoustry/catch.
Production Pitfalls
- Accidental Waterfalls: Writing
awaitinside afor..ofloop serializes independent I/O calls that should run viaPromise.allSettled(). - Dangling Rejections: Spawning a promise without attaching
.catch()in the same microtask turn firesunhandledrejection.
- When not to use
Promise.all: AvoidPromise.allwhen partial results are acceptable, one rejection aborts the batch; usePromise.allSettled()instead.
Connections & Logical Next Steps
Aborted async APIs reject their pending Promises with signal.reason on the Microtask Queue.
await scheduler.yield() suspends the async function context until the continuation task fires.
Promise fulfillment reactions always enqueue onto the Microtask Queue rather than the Task Queue.
await pops the active Execution Context to a heap frame and restores it upon resumption.