The event loop phases
The libuv phases (timers, poll, check, close), where process.nextTick and promise microtasks fit, and why setImmediate and setTimeout order differ.
The event loop phases
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The event loop is a loop with fixed phases: timers, pending callbacks, poll (I/O), check (`setImmediate`), and close callbacks. Each phase has its own queue and runs the callbacks queued for it before the loop moves on.
- `process.nextTick` callbacks and promise microtasks are not a phase. They run after the current script or callback finishes, before the loop continues. The `nextTick` queue drains first, then the microtask queue, and that repeats until both are empty.
- Inside an I/O callback, `setImmediate` always runs before a `setTimeout(fn, 0)`, because the check phase comes right after poll. From the main module the order is not guaranteed, because it depends on how long startup took.
- A timer delay is a minimum, not a promise. A callback runs when its time has passed and the loop gets back to the timers phase, which a long synchronous task can delay.
- Recursive `process.nextTick` or promise chains can starve the loop, so timers and I/O never run. Recursive `setImmediate` does not, because the loop gets a turn between each call.
From “one thread” to “a loop with rules”
The first lesson said Node runs JavaScript on one thread and hands waiting to the operating system. Something has to decide when each finished operation’s callback runs. That something is the event loop, implemented by libuv. If you know the browser’s version from the event loop and the call stack, Node’s is the same idea with more queues and its own vocabulary.
A mental model that holds up: the loop is a ring road with stops. At each stop it empties a queue, then drives to the next stop. If a stop’s queue is empty, it moves on. If there is nothing left anywhere and nothing pending, the process exits.
The phases
- Timers: runs callbacks from
setTimeoutandsetIntervalwhose time has arrived. - Pending callbacks: a few deferred system callbacks, such as some TCP errors.
- Idle, prepare: internal use.
- Poll: retrieves new I/O events and runs their callbacks (file reads finishing, sockets receiving data). If there is nothing else to do, the loop may wait here for I/O.
- Check: runs
setImmediatecallbacks, right after poll. - Close callbacks: things like
socket.on('close').
The two to remember are poll (where I/O callbacks run) and check (where setImmediate runs, immediately after poll).
The queues that are not phases
Two queues run between the stops, not at one of them:
- the
process.nextTickqueue - the promise microtask queue (promise
.then/awaitcontinuations, andqueueMicrotask)
After the main script finishes, and after every individual callback, Node drains the nextTick queue and then the microtask queue, repeating until both are empty. Only then does the loop carry on. You can watch it:
setTimeout(() => console.log('timer 1'), 0);
setImmediate(() => console.log('immediate 1'));
Promise.resolve().then(() => console.log('promise'));
process.nextTick(() => console.log('nextTick'));
console.log('main');
On Node 26.13.1 this prints main, nextTick, promise, timer 1, immediate 1. A longer experiment from the same run adds callbacks that queue more work from inside other callbacks, and shows the rule holds at every level: after timer 1 runs, its own nextTick and promise callbacks run before timer 2. Since Node 11, microtasks drain between individual timer callbacks, not just between phases, which matches how browsers behave.
setTimeout versus setImmediate
From the main module the order is not defined:
node -e "setTimeout(()=>process.stdout.write('T '),0);setImmediate(()=>process.stdout.write('I '))"
Run it several times. A 0 delay is really 1 millisecond, and whether that has passed by the time the loop first reaches timers depends on how long startup took. Inside an I/O callback the order is fixed:
import { readFile } from 'node:fs';
readFile(import.meta.filename, () => {
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
});
This always prints immediate first. The callback runs during poll. The next stop is check, where the immediate runs. The timer must wait for the loop to return to the timers phase. That is the whole difference: setImmediate means “after the current poll”, setTimeout(0) means “at least a millisecond from now, on a later timers phase”.
Timers are a floor, not a deadline
setTimeout(fn, 100) runs fn no sooner than 100 ms. If the loop is stuck in a 300 ms synchronous task, the callback runs at 300 ms or later. A server whose timers fire late is a server whose loop is blocked, which is why blocking vs non-blocking I/O ends with measuring event-loop lag.
Starvation
Because the nextTick and microtask queues must be empty before the loop moves on, a callback that keeps scheduling itself on them locks the loop:
let n = 0;
setTimeout(() => console.log('timer finally ran after', n, 'ticks'), 0);
function spin() { if (++n < 100000) process.nextTick(spin); }
spin();
The timer prints timer finally ran after 100000 ticks. Replace process.nextTick(spin) with setImmediate(spin) and it prints after only a few iterations (20 in one run, varying with timing), because every setImmediate gives the loop a chance to run a turn. If you need to split a long job into chunks, use setImmediate between them, not nextTick.
In practice, prefer promises and queueMicrotask in application code. nextTick exists for historical reasons and for the occasional library that must run before promise callbacks.
What the video covers
Software Developer Diaries’ video, from January 2026, is built around a conference talk and explains how the Node.js event loop works. Per its description it walks through the loop and the order work runs in. No transcript was available, so the phase list, the ordering rules and the code above are the lesson’s own, and each result was run against Node 26.13.1. The 2016 keynote by Bert Belder, one of libuv’s authors, is under “go deeper”. Its phase model is unchanged, but it predates the Node 11 change to when microtasks run, which is explained above.
Where this fits
This is the theory behind every async API in Node. Next, blocking vs non-blocking I/O applies it: what runs on the loop, what runs on the thread pool, and how to notice when you have blocked it. Callback-style APIs themselves are covered in error-first callbacks and promisify.
Check yourself
What order does this print: `setTimeout(() => log('timeout'), 0); setImmediate(() => log('immediate')); process.nextTick(() => log('tick')); Promise.resolve().then(() => log('promise')); log('main');`?
`main`, `tick`, `promise`, then `timeout` and `immediate` in either order. The synchronous code finishes first, then the `nextTick` queue, then promise microtasks. Timeout versus immediate from the main module depends on timing and can differ between runs.
Why does `setImmediate` always beat `setTimeout(fn, 0)` when both are scheduled from inside a `readFile` callback?
A callback from `readFile` runs in the poll phase. After poll the loop goes to the check phase, which runs `setImmediate` callbacks. The timers phase is only reached on the next trip around the loop, so the timeout waits.
What is the difference between `process.nextTick` and `queueMicrotask` or a promise `.then`?
Both run after the current operation, before the loop continues. `nextTick` has its own queue that Node drains first, so a `nextTick` callback runs before any promise callback queued at the same time. Prefer promises or `queueMicrotask` in new code, and reserve `nextTick` for the rare case where you need to run ahead of them.
A function recursively calls `process.nextTick(itself)` a hundred thousand times. A `setTimeout(..., 0)` was scheduled earlier. When does the timer run?
After all hundred thousand ticks. The `nextTick` queue must be empty before the loop moves on, so a recursive tick starves timers and I/O. Using `setImmediate` instead lets the loop run a full turn between calls, and the timer runs after a handful of them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.