Async Await in JavaScript: Internals & Execution Order

Medium100% Free~20 mins#async-await#event-loop#microtasks#promises#v8#interleaving#concurrency
Interview Answer (30-Second Summary)

In JavaScript, async/await is syntactic sugar over Promises and Generators that allows writing asynchronous code with synchronous control flow. An async function executes synchronously until its first await expression, which pauses function execution, yields control to the caller, and schedules resumption as a microtask.

Key Learning Objectives
✓

Understand how async functions execute synchronously until reaching the first await expression.

✓

Master how await transforms code into generator-like state machines and Promise microtasks.

✓

Predict exact execution ordering when async/await interleaves with Promise.then() chains.

✓

Recognize the microtask queue scheduling discipline across multiple await suspension cycles.

The Interview Problem

What is the exact sequence of outputs printed to the console when the following JavaScript code executes, and how do async/await suspension cycles interleave with Promise.then() microtasks?

1async function main() {
2 console.log('1');
3 await null;
4 console.log('2');
5 await Promise.resolve();
6 console.log('3');
7}
8
9console.log('A');
10main();
11console.log('B');
12
13Promise.resolve()
14 .then(() => {
15 console.log('C');
16 })
17 .then(() => {
18 console.log('D');
19 });
20
21console.log('E');
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

A 1 B E 2 C 3 D

A B E 1 2 3 C D

A 1 B E C D 2 3

A 1 2 3 B E C D

V8 Engine Execution Trace
Step 1 of 11 (Line 9)

Global synchronous execution begins. console.log('A') outputs 'A'.

Call Stack (Top = Active)
Global Execution Context
Lexical Scope / Bindings
main:AsyncFunctionRef#main
Console Stream
> A

Async Await in JavaScript Explained

The Mental Model: async/await as Generator + Promise State Machine

Under the hood in V8, async/await is syntactic sugar built on top of ECMAScript Generators and Promises:

  1. Synchronous Entry: Invoking an async function executes synchronously until the first await expression. It is NOT immediately queued onto the microtask queue.
  2. The await Mechanism: When await expr is evaluated:
    • expr is converted to a Promise via Promise.resolve(expr).
    • An internal reaction handler is attached to that Promise.
    • The function's execution context is suspended, preserving all local variable bindings on the heap.
    • Control yields immediately back to the caller frame.
  3. Resumption via Microtask: When the awaited promise fulfills, a microtask is pushed into the Microtask Queue to resume the async function context with the resolved value.
  4. Interleaving Discipline: Because microtasks execute in strict First-In, First-Out (FIFO) queue order, multiple async functions and .then() chains interleave tick-by-tick. Each await represents one full microtask turn.
Common Traps & Mistakes

Assuming calling an async function defers its first statement to a microtask. The first statement runs synchronously.

Sequential awaits where parallel `Promise.all` was intended: `const a = await getA(); const b = await getB();` creates unnecessary waterfall latency.

Forgetting that uncaught exceptions inside an async function reject the returned Promise rather than throwing synchronously to outer try/catch blocks.

FAANG Follow-Up Probes
Probe #1

How did the V8 engine optimize async/await in 2018 by eliminating redundant intermediate Promises (the 'Faster async functions and promises' change)?

Probe #2

How does Top-Level Await in ES2022 impact ES module graph loading and dependency execution order?

Probe #3

How do you implement an async task runner with concurrency limits (e.g., maximum 3 concurrent promises) using async/await?

Async Await in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for async/await Internals.

Q1.
How does async/await work under the hood in JavaScript engines?

Engines desugar async functions into generator-like state machines. Each await expression wraps the awaited expression in Promise.resolve(), suspends the function's execution context, and registers a microtask continuation to resume the function when the Promise settles.

No. An async function executes completely synchronously until it encounters its first await expression. Any code before the first await runs immediately on the current call stack turn.

Avoid awaiting operations sequentially if they do not depend on each other. Instead, initiate both promises simultaneously and use await Promise.all([p1, p2]) to execute them concurrently, reducing total execution time.

The engine wraps the primitive value in Promise.resolve(val) and still pauses execution, scheduling resumption as a microtask on the next turn rather than returning synchronously.

forEach does not wait for async callbacks; it executes iterations synchronously and ignores returned Promises. To run async operations sequentially, use a for...of loop; to run them concurrently, use Promise.all(items.map(async ...)).

async/await allows using standard try/catch/finally blocks. When an awaited Promise rejects, it unpacks the rejection reason and throws it as a catchable JavaScript exception.

More JavaScript interview questions for freshers