- JavaScript
- The Event Loop, Microtasks, and Asynchronous Concurrency
- async/await Internals
Async Await in JavaScript: Internals & Execution Order
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}89console.log('A');10main();11console.log('B');1213Promise.resolve()14 .then(() => {15 console.log('C');16 })17 .then(() => {18 console.log('D');19 });2021console.log('E');
Predict Console Output
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'.
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:
- Synchronous Entry: Invoking an async function executes synchronously until the first
awaitexpression. It is NOT immediately queued onto the microtask queue. - The await Mechanism: When
await expris evaluated:expris converted to a Promise viaPromise.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.
- 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.
- Interleaving Discipline: Because microtasks execute in strict First-In, First-Out (FIFO) queue order, multiple async functions and
.then()chains interleave tick-by-tick. Eachawaitrepresents 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.
Does an async function execute completely asynchronously?
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.
How do you run independent asynchronous operations concurrently with await?
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.
What happens when you await a non-Promise primitive value?
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.
Why is using await inside an Array.prototype.forEach loop an interview trap?
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 ...)).
How does error handling work with async/await compared to raw Promises?
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
