How do Promises and async/await execute inside the JavaScript Event Loop?
Quick Summary: Promises model eventual async results whose .then() and await continuations run on the high-priority Microtask Queue immediately after the synchronous call stack clears, strictly before Macrotasks like setTimeout.
Direct Interview Answer
An async function always returns a Promise. When execution reaches await expr, the function suspends cleanly, yields control back to the call stack, and schedules everything after await as a microtask once expr resolves.
Detailed Technical Explanation
A frequent bottleneck in candidate code is accidental sequential await waterfalls when requests are independent. Whenever two fetches do not depend on each other, trigger them concurrently using Promise.all() or Promise.allSettled().
Practical Walkthrough
Parallel Promise.all execution vs microtask ordering trace.
console.log("1: sync start");
setTimeout(() => console.log("4: macrotask timer"), 0);
Promise.resolve().then(() => console.log("3: microtask promise"));
console.log("2: sync end");
1: sync start 2: sync end 3: microtask promise 4: macrotask timer
Real-World Production Use Case
Preventing autocomplete race conditions with AbortController and loading dashboard widgets concurrently via Promise.allSettled without failing the entire view if one card API errors.
Common Interview Mistakes to Avoid
- Writing sequential await calls inside for-loops when concurrent Promise.all would cut latency dramatically.
- Ignoring Promise rejections (unhandledRejection) by omitting try/catch around await calls.