← Back to Node.js, page 1

nodejs-0002 · Medium mid

What are the phases of the Node.js event loop, and in what order do they run?

Each loop iteration runs these phases in order: timers, pending callbacks, idle and prepare, poll, check and close callbacks. Timers run expired setTimeout and setInterval callbacks, poll collects I/O events, check runs setImmediate callbacks, and close runs cleanup callbacks such as a socket's close event.

Full answer

The event loop is a loop inside libuv. On every turn (an iteration or "tick") it visits a fixed list of phases. Each phase has its own queue of callbacks, and the loop runs the queue until it is empty or a limit is reached, then moves on.

  1. Timers. Runs callbacks of setTimeout and setInterval whose time has passed. The delay is a minimum, not a promise.
  2. Pending callbacks. Runs some deferred system callbacks, for example certain TCP errors.
  3. Idle and prepare. Used internally by Node. You never schedule work here.
  4. Poll. Gets new I/O events and runs their callbacks (file reads, socket data). If nothing is ready, the loop may wait here, but it will not wait past the next timer, and it will not wait at all if setImmediate callbacks are queued.
  5. Check. Runs setImmediate callbacks. This is why setImmediate runs right after the poll phase.
  6. Close callbacks. Runs cleanup callbacks such as socket.on('close').

Between phases, and in modern Node between individual callbacks, Node empties the process.nextTick queue and then the promise microtask queue. These are not phases. They run whenever the current JavaScript stack finishes.

If there is no pending timer, I/O or immediate, the loop has nothing left to do and the process exits. That is why a script with only a finished promise ends by itself.

javascript
// A timer asks for 20 ms, but the script then blocks for 80 ms.
const start = Date.now();

setTimeout(() => {
  console.log('timers phase: callback ran, delay was at least 80 ms:', Date.now() - start >= 80);
}, 20);

setImmediate(() => {
  console.log('check phase: setImmediate callback');
});

while (Date.now() - start < 80) {
  // busy wait: the loop cannot start while this runs
}
console.log('main script finished');

Once the script ends, the loop starts at the timers phase. The 20 ms timer has already expired, so it runs before the check phase, even though it was also late. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): main script finished timers phase: callback ran, delay was at least 80 ms: true check phase: setImmediate callback

Key points

  • Order: timers, pending callbacks, idle/prepare, poll, check, close callbacks.
  • Timers are minimum delays. An expired timer waits until the loop reaches the timers phase.
  • Poll handles I/O, check runs setImmediate, close handles cleanup events.
  • nextTick and promise callbacks are microtask queues, not phases.

Common mistakes

  • Listing the phases but not saying what each one runs.
  • Believing setTimeout(fn, 20) runs exactly after 20 ms.
  • Treating process.nextTick as a phase of the loop.
  • Saying the loop starts at the poll phase after the main script.

Follow-ups

  • Where does the loop wait when there is nothing to do, and what stops it from waiting forever?
  • Why does Node drain the nextTick and promise queues between callbacks?
  • What happens to the loop if a callback in the poll phase takes 2 seconds?

Tips

  • Draw the six boxes and name one example callback for each.
  • State clearly that the loop is inside libuv, not inside V8.
  • Be ready to explain why a late timer is normal under load.