Skip to the page

How Node runs your code · core

The Node event loop and its phases

The Node event loop runs callbacks in phases (timers, pending callbacks, poll for I/O, check for setImmediate, close callbacks), and after every single callback it empties the process.nextTick queue and then the promise microtask queue.

The notes

What you must know

  • Phases in order: timers (setTimeout/setInterval callbacks whose time has come), pending callbacks (some deferred system I/O callbacks), idle/prepare (internal), poll (get new I/O events and run their callbacks; Node may wait here), check (setImmediate callbacks), close callbacks (for example socket 'close').
  • Poll phase: run ready I/O callbacks. If there are none and setImmediate callbacks are waiting, go on to check. If nothing is waiting, block for I/O, but only until the nearest timer is due.
  • setTimeout(fn, 0) means 'after at least 1 ms', in a timers phase. setImmediate(fn) means 'in the next check phase'.
  • In the main module, setTimeout(0) vs setImmediate order is not deterministic (it depends on how fast the process started). Inside an I/O callback, setImmediate always runs first, because check comes right after poll.
  • After each callback (since Node 11 this includes between each setTimeout and each setImmediate callback), Node runs all process.nextTick callbacks, then all promise microtasks, before the next callback.
  • Since libuv 1.45 (Node 20), timers run after the poll phase in each iteration (and once before the loop starts). The Node docs say this can change setImmediate/timer timing in edge cases.
  • The process exits when nothing keeps the loop alive: no timers, open sockets, listening servers or other active handles. handle.unref() tells Node not to wait for a handle.

The red pen

Common traps: mistake → fix

  • Mistake: The Node event loop is the same as the browser's

    Fix: both have tasks and microtasks, but Node has libuv phases, process.nextTick and setImmediate, and no rendering step.

  • Mistake: setTimeout(fn, 0) runs right after the current code

    Fix: it waits for a timers phase and at least 1 ms; nextTick and promise callbacks always run before it.

  • Mistake: setImmediate runs immediately

    Fix: it runs in the check phase; process.nextTick runs sooner. The Node docs admit the names are swapped.

  • Mistake: setTimeout(0) always beats setImmediate

    Fix: that is chance in the main module; inside an I/O callback setImmediate always wins.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the event loop in Node and why does Node need one?
  2. Name the phases in order. What happens in the poll phase?
  3. setTimeout(0) vs setImmediate: which runs first? Is it always the same?
  4. Where do promise callbacks and process.nextTick run relative to the phases?
  5. Why does a script exit by itself but a server keeps running?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview