Full Byte Developer.

Microtasks, starvation, and Node versus browser

Node nextTick can run before or after promises in CJS vs ESM. Cap starvation demos.

Published here · 5 sources

Part 2 of the JS event loop series. Part 1 is the promises and event loop quick start.

After each macrotask, the runtime drains microtasks (promise .then and queueMicrotask) before the next timer or other task [1] [2] [5]. Node adds process.nextTick, which drains after the current operation and before the event loop continues [3] [4]. The catch on modern Node is that CommonJS and ESM can print different orders for the same three calls.

Cap a nextTick spin so it cannot starve I/O

Recursive nextTick can keep the event loop from reaching I/O [3]. Use a hard iteration cap:

bash
node -e "
let n = 0;
function spin() {
  if (n++ < 5) process.nextTick(spin);
  else console.log('spin done');
}
spin();
Promise.resolve().then(() => console.log('promise after spin'));
"

You should see:

text
spin done
promise after spin

In this CJS run, the capped nextTick chain finishes before the promise. Do not publish an uncapped loop.

Contrast the browser path without nextTick

Browsers have no native process.nextTick. In a browser console, sync work runs, then microtasks, then the timer [1] [5]:

javascript
console.log('sync');
setTimeout(() => console.log('timeout'), 0);
Promise.resolve().then(() => console.log('promise'));
queueMicrotask(() => console.log('queueMicrotask'));

You should see sync, then promise, then queueMicrotask, then timeout (promise was queued before queueMicrotask). Exact setTimeout(0) delay floors vary by browser; treat that as orientation, not a millisecond guarantee.

For the Part 1 demos that stay identical across browser and Node for promises and timers only, return to JavaScript promises and the event loop (quick start).

Run the CJS and ESM one-liners on your Node version before you memorize either order.

Send this to the friend confused why nextTick and promises run in a different order in Node.

Two annotated console traces of the same three calls on Node.js 22.23.4, 14 Nov 2026: setTimeout 0, process.nextTick, and Promise.then. In CJS, run with node -e, the printed order is nextTick, promise, timeout. In ESM, run with node --input-type=module, the order is promise, nextTick, timeout. Official Node docs: in CJS, nextTick before queueMicrotask; in ESM the reverse can happen. process.nextTick is Legacy; prefer queueMicrotask. Browsers have no process.nextTick.
Do not memorize one Node order. CJS and ESM disagree on nextTick versus promises. Browsers have no nextTick.
Transcript: Bounded nextTick spin before a promise

Short looping illustration of a bounded CJS nextTick spin on Node.js 22.23.4. A function queues another nextTick while a counter is under 5. The nextTick queue finishes first and prints spin done. Only then does the scheduled promise print promise after spin. Official Node docs warn that unbounded recursive nextTick can infinite-loop and starve I/O, so the demo stops at 5.

Open video file

In CJS, recursive nextTick work runs before promise jobs. Cap the recursion. Unbounded loops can starve I/O.

Keep this guide

By Full Byte Developer

Filed under Dev explainers

Questions about this guide

Why does nextTick beat a promise in one Node script and lose in another?

Module mode. Node docs say that in CommonJS, nextTick callbacks run before queueMicrotask or promise jobs in the simple case. In ESM, promises and microtasks can run first because ESM evaluation already participates in microtask processing. The demos below show both orders on Node 22.23.4.

Do browsers have process.nextTick?

No. Browsers schedule promise handlers and queueMicrotask callbacks as microtasks after the current task, then run macrotasks such as setTimeout. There is no native process.nextTick.

Is an infinite nextTick loop a useful demo?

No. Node warns that recursive nextTick can starve I/O. Publish only a hard-capped spin, such as five iterations.

Sources

Next in this stack