← Back to JavaScript, page 5

javascript-0096 · Hard senior

In Node.js, in what order do process.nextTick, promise callbacks, setTimeout, and setImmediate run, and why does it depend on where they are called?

After the current operation finishes, Node runs the process.nextTick queue first, then promise microtasks, before moving on. The event loop then goes through phases: timers, pending callbacks, poll (I/O), check (setImmediate), and close callbacks. From the main script, setTimeout 0 versus setImmediate is not guaranteed, but inside an I/O callback setImmediate always runs before setTimeout 0, because the check phase follows the poll phase.

Full answer

Node.js has two kinds of queues. Microtask-like queues run between operations, and the event loop phases run in a fixed cycle.

After each callback finishes, and after the main script, Node drains process.nextTick callbacks first. Then it drains promise reactions (microtasks). If a microtask adds a nextTick, Node goes back and runs it before moving on. This is why a loop of recursive nextTick calls can starve I/O: the loop never reaches the next phase.

The event loop phases are, in order:

  1. Timers: runs expired setTimeout and setInterval callbacks.
  2. Pending callbacks: some deferred system callbacks.
  3. Idle and prepare: internal.
  4. Poll: waits for and runs I/O callbacks, such as a finished file read or incoming data. If setImmediate callbacks are queued, it does not block.
  5. Check: runs setImmediate callbacks.
  6. Close callbacks: for example a socket close event.

A timer with 0 ms is actually 1 ms, and it fires only when the loop reaches the timers phase and the time has passed. From the main script, the loop may start before 1 ms has passed, so setTimeout(..., 0) and setImmediate can run in either order, and it varies with machine load. Never rely on it.

Inside an I/O callback, the loop is in the poll phase. The next phase is check, so setImmediate always runs first. The timer waits for the next loop cycle. That is the one deterministic case, and the snippet shows it with fs.readFile.

The snippet also shows that inside an I/O callback, after the callback returns, nextTick comes first, then promise callbacks, and only then the immediate and the timer.

In ES modules, the main module runs inside a promise job, so promise callbacks run before nextTick callbacks queued by the main script. In CommonJS, nextTick runs first.

Practical advice: use setImmediate or a promise to yield to I/O, avoid recursive nextTick, and do not design code around the order of a 0 ms timer and an immediate in the main script.

javascript
const fs = require('node:fs');

console.log('1 main script start');

process.nextTick(() => console.log('3 nextTick from main'));
Promise.resolve().then(() => console.log('4 promise from main'));

console.log('2 main script end');

// Wait for the I/O callback, then check the order inside it.
// The timer and the immediate are scheduled from inside the I/O callback,
// where the order is guaranteed.
fs.readFile(__filename, () => {
  console.log('5 I/O callback');
  setTimeout(() => console.log('9 timeout 0 (next loop iteration)'), 0);
  setImmediate(() => console.log('8 setImmediate (check phase)'));
  process.nextTick(() => console.log('6 nextTick from I/O callback'));
  Promise.resolve().then(() => console.log('7 promise from I/O callback'));
});

Run with Node.js v24.22.1 as js-96.js; two runs gave identical output. Output: 1 main script start 2 main script end 3 nextTick from main 4 promise from main 5 I/O callback 6 nextTick from I/O callback 7 promise from I/O callback 8 setImmediate (check phase) 9 timeout 0 (next loop iteration)

Key points

  • nextTick queue first, then promise microtasks, after each callback and after the main script.
  • Phases: timers, pending, poll (I/O), check (setImmediate), close.
  • Main script: setTimeout 0 versus setImmediate order is not guaranteed.
  • Inside an I/O callback, setImmediate always runs before setTimeout 0.
  • Recursive nextTick can starve the loop; ESM and CommonJS differ for nextTick versus promises in the main module.

Common mistakes

  • Assuming setTimeout 0 always runs before setImmediate, or the reverse, from the main script.
  • Writing recursive process.nextTick calls that block I/O.
  • Thinking promises and nextTick are the same queue.
  • Using setImmediate and process.nextTick names as if they described when they run.

Follow-ups

  • Why does recursive setImmediate not starve I/O the way recursive nextTick does?
  • Where do the callbacks of the worker thread pool, such as fs and crypto, come back into the loop?
  • What does queueMicrotask do compared with process.nextTick?

Tips

  • State the order: nextTick, promise microtasks, then phases.
  • Name the one deterministic case: setImmediate before setTimeout 0 inside an I/O callback.
  • Warn about starvation from recursive nextTick.