Skip to content
jshacks.dev
Async

process.nextTick vs queueMicrotask

Compare process.nextTick and queueMicrotask on Node.js 24, see which runs first, how each can starve the event loop and which one new code should use.

6 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the JavaScript, Node.js, npm and TypeScript logos.

Node.js has two ways to say "run this right after the current operation", and they are not the same. process.nextTick is older, specific to Node.js and drains its own queue before the promise microtask queue. queueMicrotask is standard, works in browsers and joins the same queue as promise callbacks. For most new code, queueMicrotask is the right choice, and the Node.js documentation says so. But process.nextTick is still everywhere in Node.js core and in libraries, and the difference in ordering decides whether a piece of code sees state before or after a promise handler. This post explains both on Node.js 24 with printed output, shows the ordering rules and the starvation risk each carries, and ends with guidance on which to use.

Two queues, one priority rule

After the current operation completes, Node.js runs the nextTick queue first, to completion. Then it runs the microtask queue, which holds promise callbacks and queueMicrotask functions, to completion. If draining microtasks adds new nextTick callbacks, those run after the microtask queue empties, and the cycle repeats until both are empty. Only then does the event loop go on to the next phase.

In a CommonJS file:

JavaScript
Promise.resolve().then(() => console.log("promise"));
queueMicrotask(() => console.log("queueMicrotask"));
process.nextTick(() => console.log("nextTick"));

console.log("sync");
Text
sync
nextTick
promise
queueMicrotask

nextTick ran first even though it was registered last. The promise callback and the queueMicrotask callback keep the order they were queued in, because they share the microtask queue. The rule is simple, and it is the reason nextTick is described as running "sooner" than a promise.

The CommonJS and ES module difference

The ordering above is for a CommonJS script. In an ES module, the main module body itself runs as part of a promise job, so by the time it finishes, the microtask queue is already being drained, and promise callbacks run before nextTick callbacks.

JavaScript
// order.mjs
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");
Text
sync
promise
nextTick

This reverses the result for the same code. It is a good reason not to depend on the relative order of the two in code that might run as either module type. Inside later callbacks, such as an I/O callback or a timer, both module types behave the same way, with nextTick first.

Where nextTick came from

process.nextTick predates promises in Node.js. It was the way to make a callback API consistently asynchronous, and the way to let a caller attach listeners after an object is constructed but before it emits an event.

JavaScript
import { EventEmitter } from "node:events";

class Job extends EventEmitter {
  constructor() {
    super();
    process.nextTick(() => this.emit("ready"));
  }
}

const job = new Job();
job.on("ready", () => console.log("ready event handled"));
Text
ready event handled

If the constructor emitted ready synchronously, no listener would exist yet and the event would be lost. Deferring it with nextTick, or equally with queueMicrotask, gives the caller time to subscribe. Node.js core uses this pattern in many places.

The same job with queueMicrotask

JavaScript
import { EventEmitter } from "node:events";

class Job extends EventEmitter {
  constructor() {
    super();
    queueMicrotask(() => this.emit("ready"));
  }
}

const job = new Job();
job.on("ready", () => console.log("ready event handled"));
Text
ready event handled

The result is the same, and the code also runs in a browser. That portability, plus the standard specification, is why new code should lean on queueMicrotask.

Starvation: both can do it

Each queue drains completely, so a callback that keeps scheduling itself blocks the event loop. With nextTick, it starves even promise callbacks, since the tick queue is always processed first.

This example is a CommonJS file, since the ordering differs in an ES module.

JavaScript
let ticks = 0;

function spin() {
  ticks += 1;
  if (ticks < 100000) process.nextTick(spin);
}

Promise.resolve().then(() => console.log("promise ran after", ticks, "ticks"));
setTimeout(() => console.log("timer ran after", ticks, "ticks"), 0);

process.nextTick(spin);
Text
promise ran after 100000 ticks
timer ran after 100000 ticks

The bound makes it finish. Without the bound, neither the promise callback nor the timer would ever run. process.nextTick therefore has a sharper failure mode than queueMicrotask: it can starve promise callbacks too, while a runaway microtask blocks only the macrotasks.

Node.js's documentation warns about this, and there is no guard in the runtime. If you need recursion that yields, use setImmediate.

Errors

An exception thrown inside either callback is an uncaught exception. Node.js reports it and exits, unless an uncaughtException handler exists.

JavaScript
process.on("uncaughtException", (error) => {
  console.log("uncaught:", error.message);
});

queueMicrotask(() => {
  throw new Error("from a microtask");
});

process.nextTick(() => {
  throw new Error("from a tick");
});
Text
uncaught: from a tick
uncaught: from a microtask

Unlike a throw inside a promise callback, which becomes a rejection, a throw in queueMicrotask is reported as an uncaught exception, the same as a throw in a tick. The order shown is for a CommonJS file. If you want rejection semantics, schedule with Promise.resolve().then(fn), or catch inside the callback.

Arguments

process.nextTick accepts extra arguments and passes them to the callback, which avoids a closure. queueMicrotask takes only the function.

JavaScript
process.nextTick((a, b) => console.log("sum:", a + b), 2, 3);
queueMicrotask(() => console.log("sum:", 4 + 5));
Text
sum: 5
sum: 9

Which to use

Use queueMicrotask in new code. It is a web standard, it works the same in browsers, Deno and Bun, and it has a smaller blast radius when something goes wrong. The Node.js documentation recommends it as the more portable choice, and the main reason to use nextTick now is to match Node.js core's own ordering inside code that must run before promise callbacks, such as an API that emits an event on construction and whose contract promises it fires before any promise settled in the same turn. If you maintain a library that supports older Node.js versions, queueMicrotask has been available since Node.js 11, so that is rarely a constraint any more.

Prefer neither when a macrotask would do. If the goal is to let the event loop breathe, setImmediate is correct, since both queues here run to exhaustion before it.

Migrating old code

If your code base uses process.nextTick everywhere, there is no urgency to replace it. Both work, and the ordering rules are stable. A migration is worth doing when you want the same code to run in a browser or another runtime, or when you notice that a library mixes the two and produces different orderings depending on the module system. Replace calls one at a time, run the tests and look for any case where the code depended on running before a promise callback. Those cases usually need a rethink, since an ordering that depends on queue priority is fragile by design.

Reading Node.js core and library code

You will see process.nextTick far more often than you will write it, mostly in streams, net, http and in packages that were written before promises. When you read it, the question to ask is what the author needed to happen first. Usually the answer is that an event must fire only after the caller has had time to attach a listener, or that a callback must never run in the same turn as the call that scheduled it. Both goals are met equally well by queueMicrotask, and the difference only shows up in the relative order against promise callbacks.

If you debug an ordering problem in code that mixes both, print a label from each callback and run the file as CommonJS and as an ES module. The two runs will differ exactly where nextTick and a promise compete, which pinpoints the lines to fix. Replacing one with the other in that spot, and adding a comment that states the required order, is usually enough.

Also inNode.js
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.