# Microtasks, starvation, and Node versus browser

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

Source: https://fullbytedeveloper.com/blog/javascript-microtasks-node-vs-browser
Published here: 2026-11-17

Part 2 of the JS event loop series. Part 1 is the [promises and event loop quick start](/blog/javascript-promises-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`:

```bash
node -e "
setTimeout(() => console.log('timeout'), 0);
process.nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));
"
```

You should see:

```text
nextTick
promise
timeout
```

That 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`:

```bash
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:

```text
promise
nextTick
timeout
```

Node 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:

```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)](/blog/javascript-promises-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.

Image: 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.
https://fullbytedeveloper.com/media/javascript-microtasks-node-vs-browser/cjs-vs-esm.webp
Do not memorize one Node order. CJS and ESM disagree on nextTick versus promises. Browsers have no nextTick.

Video: Bounded nextTick spin before a promise
https://fullbytedeveloper.com/media/javascript-microtasks-node-vs-browser/nexttick-drain.webm
In CJS, recursive nextTick work runs before promise jobs. Cap the recursion. Unbounded loops can starve I/O.
Poster: https://fullbytedeveloper.com/media/javascript-microtasks-node-vs-browser/cjs-vs-esm.webp
Transcript: 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.

## Sources

1. [MDN: queueMicrotask, accessed 2026-11-14](https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask)
2. [javascript.info: Event loop (updated 2024-05-17), accessed 2026-11-14](https://javascript.info/event-loop)
3. [Node.js: process.nextTick, accessed 2026-11-14](https://nodejs.org/api/process.html#processnexttickcallback-args)
4. [Node.js: The Node.js Event Loop, accessed 2026-11-14](https://nodejs.org/learn/asynchronous-work/event-loop-timers-and-nexttick)
5. [MDN: JavaScript execution model, accessed 2026-11-14](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model)
