Sub topic 13
The Node.js Event Loop and Its Phases
This chapter assumes you already know the browser model from earlier chapters: the call stack, tasks, the microtask queue, and how promise reactions run as microtasks. It also assumes you can run a script with node file.js.
Node.js uses the same basic idea as the browser: one thread runs your JavaScript, and a loop feeds it callbacks. The difference is in how Node.js organizes the waiting work. The browser treats it as a set of task queues. Node.js splits each turn of the loop into phases. A phase is a stage with its own queue of callbacks, and phases always run in a fixed order. This loop comes from libuv, the C library Node.js uses for asynchronous I/O.
The Six Phases
Each turn of the loop moves through these phases in order:
| Phase | What runs there |
|---|---|
| timers | `setTimeout` and `setInterval` callbacks whose delay has passed |
| pending callbacks | Some system-level I/O callbacks deferred from the last turn, such as certain TCP errors |
| idle, prepare | Internal use by Node.js only |
| poll | Most I/O callbacks (file reads, sockets). The loop may wait here for new I/O |
| check | `setImmediate` callbacks |
| close callbacks | `close` events, such as `socket.on('close', ...)` |
When the loop enters a phase, it runs the callbacks in that phase's queue until the queue is empty or a system limit is reached. Then it moves to the next phase.
The Poll Phase Decides the Pace
The poll phase is where Node.js spends most idle time. If no callbacks are waiting there, the loop decides what to do next:
- If
setImmediatecallbacks are queued, it leaves poll and goes to the check phase. - Otherwise, it blocks and waits for I/O, but only until the nearest timer is due. Then it wraps around to the timers phase.
If no timers, I/O, or immediates are pending at all, the loop exits and the process ends.
Where Microtasks Fit
Microtasks are not a phase. Since Node.js 11, Node.js drains the process.nextTick queue and then the promise microtask queue after each individual callback, not only between phases. This matches browser behavior. The next chapter covers the nextTick queue in detail.
setTimeout(() => {
console.log('timeout 1');
Promise.resolve().then(() => console.log('microtask after timeout 1'));
}, 0);
setTimeout(() => console.log('timeout 2'), 0);
// Output (Node.js 11+):
// timeout 1
// microtask after timeout 1
// timeout 2Both timers are in the timers phase of the same turn. The promise reaction still runs between them, because Node.js drains microtasks as soon as the first callback returns.
A Common Misreading
A timer delay is the earliest time a callback can run. Node.js checks timers only in the timers phase. If a slow poll-phase callback is running when a timer comes due, the timer waits for the next turn of the loop.
Exercise
A file read finishes while the loop is in the timers phase. It is the only pending I/O, and no setImmediate callbacks are queued. In which phase does its callback run, and what runs right after the callback returns?
Show answer
The callback runs in the poll phase of that same turn, because the loop passes through pending callbacks and idle/prepare before it reaches poll. As soon as the callback returns, Node.js drains the process.nextTick queue and then the promise microtask queue. Only after that does the loop take the next poll callback or move on to the check phase.
Exercise
Why can a script that only calls setTimeout(fn, 5000) keep the process alive for five seconds, while an empty script exits at once?
Show answer
The pending timer gives the loop work to do, so the poll phase waits until the timer is due rather than exiting. With no timers, I/O, or immediates pending, the loop has nothing left and the process exits.