
“Explain the event loop” is one of the most asked JavaScript interview questions I’ve seen. It came up in some form in almost every loop I went through, either directly or hidden inside an “what does this code print?” puzzle.
Most explanations I read when I was preparing were either too shallow (“JavaScript is single-threaded and uses callbacks”) or went straight into the HTML spec. Here’s the version I wish I’d had: one diagram, the rules that actually matter, and the puzzles interviewers love.
The diagram

JavaScript runs on one main thread with one call stack. Things like timers, network requests, and DOM events are handled by the browser outside of JavaScript. When they’re ready, they don’t interrupt your code. They put a callback in a queue, and the event loop picks it up when the stack is empty.
The whole thing comes down to three rules.
Rule 1: Run one task to completion
A task (often called a macrotask) is a unit of work: running a script, a setTimeout callback, a click handler, a message event. The event loop takes one task from the queue and runs it until the call stack is empty. Nothing can interrupt it.
That’s why a long-running loop freezes the page. Clicks still get queued, but nothing can run them until your task finishes.
Rule 2: Drain all microtasks
After each task, the event loop runs every microtask in the microtask queue, including any new ones added while it’s draining. Microtasks come from:
- Promise reactions:
.then(),.catch(),.finally() - The code after an
await queueMicrotask()MutationObservercallbacks
“Every” is the important word. One task, then all the microtasks. That’s why promises always beat setTimeout.
Rule 3: Render if it’s time
After microtasks, the browser may render a frame. It runs requestAnimationFrame callbacks, then recalculates styles, does layout, and paints. This typically happens around 60 times per second (more on high refresh rate screens), and only if something changed.
Notice where rendering sits: after the task and its microtasks. If your task takes 300ms, the screen doesn’t update for 300ms. That’s exactly what the INP metric measures.
The classic puzzle
Try it before reading the answer:
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
(async () => {
console.log('5');
await null;
console.log('6');
})();
console.log('7');
Output: 1, 5, 7, 3, 4, 6, 2
Walk through it with the rules:
- The whole script is one task.
1logs. setTimeouthands the callback to the browser’s timer. It’ll become a task later..thenqueues microtask3.queueMicrotaskqueues microtask4.- The async function runs synchronously until its first
await, so5logs right away. The rest of the function (6) is queued as a microtask. 7logs. The script task is done.- Drain microtasks in order:
3,4,6. - Next task: the timer callback,
2.
The part that trips people up most is step 4. An async function is not asynchronous until it hits an await.
The harder follow-up
Interviewers often push further:
setTimeout(() => console.log('timeout'), 0);
requestAnimationFrame(() => console.log('raf'));
Promise.resolve().then(() => {
console.log('micro 1');
Promise.resolve().then(() => console.log('micro 2'));
});
micro 1, then micro 2, because microtasks queued during draining still run before the loop moves on.
Then timeout and raf in either order. It depends on whether the browser decides to render a frame before picking up the timer task. If you say “it depends, and here’s why”, that’s a much stronger answer than guessing one confidently.
setTimeout(fn, 0) isn’t zero
A few things worth knowing:
- The delay is a minimum, not a guarantee. The callback runs when its task gets picked up, after whatever is ahead of it.
- Browsers clamp nested timers. Once timers are nested more than 5 levels deep, the minimum delay becomes 4ms.
- Background tabs throttle timers much more aggressively, sometimes to once per second or less.
So never use setTimeout for precise timing. For animations, use requestAnimationFrame.
Microtask starvation
Because microtasks drain completely, you can freeze the page without a single long loop:
function forever() {
Promise.resolve().then(forever);
}
forever(); // the page never renders again and clicks never run
The same code using setTimeout(forever, 0) wouldn’t freeze the page, because each call becomes a separate task, and the browser can render and handle input between tasks.
This is a nice point to bring up in an interview. It shows you understand why the queues are different, not just which one runs first.
Practical takeaways
| You want to… | Use | Why |
|---|---|---|
| Run code right after the current work, before rendering | queueMicrotask / promise |
Runs before the next paint |
| Let the browser render and handle input first | setTimeout or scheduler.yield() |
Creates a new task, so rendering can happen in between |
| Update something visually every frame | requestAnimationFrame |
Runs right before the browser paints |
| Do low-priority work when the browser is idle | requestIdleCallback |
Runs only when there’s spare time in a frame |
| Do heavy computation | A Web Worker | Separate thread, separate event loop |
A quick note on Node.js
Node also has an event loop, but it’s structured differently (it has phases for timers, I/O, and setImmediate). Two things to remember for interviews: process.nextTick callbacks run before promise microtasks, and setImmediate vs setTimeout(fn, 0) ordering isn’t guaranteed from the main module. If the role is frontend-focused, they’ll almost always be asking about the browser.
How I’d answer “Explain the event loop”
“JavaScript runs on a single thread with one call stack. Async work like timers and network requests is handled by the browser, which queues a callback when it’s ready. The event loop takes one task from the task queue and runs it to completion, then drains every microtask, like promise callbacks and code after await, and then the browser may render a frame. That’s why promises run before setTimeout, and why a long task blocks both rendering and input.”
That’s about 30 seconds, and it covers the stack, the queues, the ordering rule, and the performance implication. Then let them ask follow-ups.
If you want to practice more, check out the free trivia questions on the event loop and micro vs macro tasks, and coding questions like build a promise that test this in practice.
And that’s it, folks. One task, all the microtasks, maybe a render, repeat. Once that clicks, most of those “what does this print?” puzzles become easy points.
