Back
JavaScriptHardNew
Predict the output: event loop ordering with sync code, Promises, and setTimeout
Sync first, then drain microtasks, then macrotasks.
Ideal Answer
What prints and in what order?
JavaScript
console.log('A')
setTimeout(() => console.log('B'), 0)
Promise.resolve()
.then(() => console.log('C'))
.then(() => console.log('D'))
console.log('E')
Answer: A → E → C → D → B
Step by step
- Sync phase (call stack):
A, schedule macrotaskB, schedule microtasks for Promise chain,E. - Microtask drain: First
.thenruns →C. That schedules another microtask →D. - Microtask drain continues:
Druns. - Macrotask:
setTimeoutcallback →B.
Rules to remember
- Synchronous code always runs first.
- The microtask queue is fully drained before the next macrotask.
- New microtasks scheduled during microtask processing run in the same turn.
setTimeout(fn, 0)is not instant — it waits for the stack to clear and all microtasks.
Variant trap: If the second .then did setTimeout(() => log('F'), 0), F would print after B because it is scheduled in a later macrotask turn.