Node.js Reference ยท Events and Timers
Timers vs nextTick vs setImmediate in Node.js
Reference for execution order in Node.js: synchronous code, process.nextTick(), promise microtasks, setTimeout() and setImmediate(), with runnable proofs.
Node.js has several ways to say "run this later": process.nextTick(), promise callbacks and queueMicrotask(), setTimeout(), and setImmediate(). They are not interchangeable. Each belongs to a different queue, and the queues are drained in a fixed pattern around the event loop.
The rule of thumb is: the current synchronous code finishes first; then the nextTick and microtask queues are emptied; then the event loop moves on to timers, I/O callbacks and the check phase where immediates run. Between every callback of the loop, the nextTick and microtask queues are drained again. The details below explain the places where the order is fixed and the one place where it is not.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
The queues, one by one
- Synchronous code runs to completion. Nothing else can interrupt it.
process.nextTick()queue is drained as soon as the current operation ends, before promise microtasks in CommonJS. A callback that schedules anothernextTickkeeps the queue going, so it can starve the event loop.- Microtask queue holds promise reactions and
queueMicrotask()callbacks. It is drained right after the nextTick queue, and again whenever new microtasks appear. - Timers phase runs
setTimeout()andsetInterval()callbacks whose time has come. - Poll phase runs I/O callbacks and waits for new I/O.
- Check phase runs
setImmediate()callbacks.
Usage notes
- ES modules differ from CommonJS. An ES module body is evaluated as a promise job, so when it finishes, the microtask queue drains first and
nextTickcallbacks run after promise callbacks. In a CommonJS file the order is reversed. The third example shows the module case. setTimeout(fn, 0)versussetImmediate(fn)from the main module has no guaranteed order: it depends on how long process start took relative to the 1 ms minimum timer delay. Never write code that relies on it.- From inside an I/O callback,
setImmediate()always runs before a zero-delay timer, because the loop is in the poll phase and the check phase comes next. - Timers are lower bounds.
setTimeout(fn, 10)runs no sooner than 10 ms, and later if the loop is busy. - Choose by purpose.
nextTickfor "after this operation, before anything else" (for example to emit an event after the caller has attached a listener);queueMicrotask()or promises for ordinary async work;setImmediate()to let I/O run first;setTimeout()for real delays. - Starvation: recursive
nextTickor microtask scheduling prevents timers and I/O from ever running. RecursivesetImmediate()does not, since the loop turns between calls. - Prefer
queueMicrotask()overprocess.nextTick()in new code unless you need its early timing; the Node.js documentation also recommendssetImmediate()for most deferral needs.