Full answer
JavaScript has a single main thread. Code runs to completion on the call stack, so two pieces of your code never run at the same time. Things that wait, such as timers, network calls, and file reads, are handled outside the stack by the browser or by Node.js. When they finish, they put a callback into a queue.
The event loop is the rule that decides what runs next. After the current script (which is itself one macrotask) ends and the stack is empty, the loop first drains the whole microtask queue. Promise reactions, queueMicrotask callbacks, and await continuations are microtasks. If a microtask adds another microtask, that one also runs before the loop moves on. Only then does the loop take the next macrotask, such as a setTimeout callback, and the cycle repeats. In a browser, rendering gets a chance between macrotasks.
Two consequences matter in senior interviews. First, a promise callback always runs before a setTimeout(fn, 0) that was scheduled in the same turn. Second, microtasks can starve the loop: a microtask that keeps scheduling new microtasks will never let a timer, a click, or a repaint happen. Likewise, a long synchronous loop freezes the page for its full duration. The fix is to split work into chunks and yield with a macrotask.
Node.js adds process.nextTick, which runs before promise microtasks, and setImmediate, which runs in a later phase of its loop.
console.log('1 sync start');
setTimeout(() => console.log('6 macrotask: timeout 0'), 0);
Promise.resolve().then(() => {
console.log('3 microtask: promise');
queueMicrotask(() => console.log('5 microtask added by a microtask'));
});
queueMicrotask(() => console.log('4 microtask: queueMicrotask'));
(async () => {
console.log('2 sync part of async function');
await null;
console.log('4b continuation after await');
})();
console.log('2b sync end');Run with Node.js v24.22.1 as js-6.js; two runs gave identical output. Output: 1 sync start 2 sync part of async function 2b sync end 3 microtask: promise 4 microtask: queueMicrotask 4b continuation after await 5 microtask added by a microtask 6 macrotask: timeout 0
Key points
- One call stack, one thread; callbacks wait in queues until the stack is empty.
- After each macrotask, the loop drains all microtasks before the next macrotask.
- Promise callbacks and await continuations are microtasks; timers and I/O callbacks are macrotasks.
- setTimeout delay is a minimum, not a guarantee.
- Endless microtasks or long synchronous work block timers, input, and rendering.
Common mistakes
- Saying setTimeout with 0 runs immediately. It runs after the stack and all microtasks are clear, and browsers clamp nested timers to about 4 ms.
- Believing JavaScript runs promises in parallel. The work behind a promise may run elsewhere, but its callbacks still run one at a time on the main thread.
- Calling the event loop "the queue". There are several queues with different priority.
- Putting heavy computation in a promise callback and expecting the UI to stay responsive.
Follow-ups
- Where does process.nextTick fit relative to promise callbacks in Node.js?
- How would you process 100,000 records without freezing the browser?
- Why can a recursive promise chain block a setTimeout callback forever?
Tips
- Walk through a snippet line by line out loud. Interviewers want to hear the queues named as you go.
- Mention starvation and chunking; that is what separates a senior answer from a textbook one.
- Say the browser and Node.js loops differ in detail, then give the one difference you know (nextTick and setImmediate).