Interview Prepintermediate
Explaining the event loop in an interview
Microtasks vs macrotasks, and why a Promise callback always beats a setTimeout(0).
Introduction
The call stack runs synchronous code. When it empties, the loop drains the microtask queue (Promises, queueMicrotask, MutationObserver) completely, then takes exactly one macrotask (timers, I/O, events) and repeats.
That is why a resolved Promise callback runs before a setTimeout(0) queued at the same time.
- Microtasks: Promise.then, await continuations, queueMicrotask.
- Macrotasks: setTimeout, setInterval, DOM events, message channel.
- Rendering happens between macrotasks, after microtasks drain.