JS RuntimeBaseline · Widely

Promises and async/await Internals

Mental Model

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.

Interactive Simulator

Async Function Suspension & Microtask Resume

Trace how await suspends an execution context and resumes via the Microtask Queue.

Call Stack
Suspended Context
Microtask Queue
Step 1 / 3: 1. Synchronous Prologue

async fn() runs synchronously on the Call Stack until it hits await.

console.log('A');
const data = await fetchJSON();
Internal State / Memory
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.

  1. Synchronous Prologue: Calling async fn() runs straight on the Call Stack until an await expression.
  2. Context Suspension: await wraps the operand in Promise.resolve(), attaches a hidden resume reaction, and pops the frame off the Call Stack.
  3. Microtask Continuation: Even await 1 yields at least one tick to the Microtask Queue before restoring the local variables.
  4. Zero-Cost Async Stack: V8 reconstructs async stack traces by walking promise reaction links without keeping dead stack frames alive.
JS
// 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/catch and 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 synchronous try/catch.

Production Pitfalls

Production Pitfall
  • Accidental Waterfalls: Writing await inside a for..of loop serializes independent I/O calls that should run via Promise.allSettled().
  • Dangling Rejections: Spawning a promise without attaching .catch() in the same microtask turn fires unhandledrejection.
When Not to Use
  • When not to use Promise.all: Avoid Promise.all when partial results are acceptable, one rejection aborts the batch; use Promise.allSettled() instead.

Connections & Logical Next Steps