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 · Resume reading
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.
Print the CJS nextTick versus promise order
On Node 22.23.4 in this environment, CommonJS prints nextTick, then promise, then timeout:
node -e "
setTimeout(() => console.log('timeout'), 0);
process.nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));
"You should see:
nextTick
promise
timeoutThat matches the Node docs note that in CJS, nextTick callbacks run before queueMicrotask or promise jobs in the simple case [3].
Print the ESM order on the same three calls
Same three schedules, ESM input. On Node 22.23.4 this environment printed promise, then nextTick, then timeout:
node --input-type=module -e "
import { nextTick } from 'node:process';
setTimeout(() => console.log('timeout'), 0);
nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));
"You should see:
promise
nextTick
timeoutNode docs explain the flip: ESM evaluation already participates in microtask processing, so promises and microtasks can run before nextTick [3]. Prefer queueMicrotask when you want portable microtask scheduling; Node marks process.nextTick as Legacy on that same page [3].
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:
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:
spin done
promise after spinIn 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]:
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.

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.
Keep this guide
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
- MDN: queueMicrotask, accessed 2026-11-14developer.mozilla.org
- MDN: JavaScript execution model, accessed 2026-11-14developer.mozilla.org