BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Posts/javascript
Advanced3 min read

Microtask vs Macrotask in JavaScript — With Node.js vs Browser Notes

Understand microtasks and macrotasks across browsers and Node.js, including process.nextTick, setImmediate, and event loop phases.

Mohsen Fallahnejad

Mohsen Fallahnejad

Sep 20, 2025Discuss

Recap (Browser)#

  • Microtasks: Promise.then/catch/finally, queueMicrotask, MutationObserver
  • Macrotasks: setTimeout, setInterval, requestAnimationFrame, DOM events
  • Order per tick: run script → drain all microtasks → possibly render → then the next macrotask.

Node.js Event Loop Differences#

Node.js has phases in its event loop (simplified):

  1. Timers → setTimeout / setInterval callbacks
  2. Pending Callbacks
  3. Poll → I/O callbacks; may block waiting for I/O
  4. Check → setImmediate callbacks
  5. Close Callbacks

After each phase, Node runs microtask queues in this order:

  • process.nextTick queue (✅ runs before promise microtasks)
  • Promise microtasks (jobs scheduled by Promise.then / queueMicrotask)

Key takeaways#

  • process.nextTick always runs before other microtasks and before moving to the next phase. Overuse can starve the loop.
  • setImmediate runs in the Check phase.
  • setTimeout(..., 0) runs in the Timers phase of a later tick.
  • Order of setTimeout(…,0) vs setImmediate can vary:
    • After I/O, setImmediate tends to run first (since the loop is already at the Check phase).
    • Without I/O, timers may run first (implementation- and timing-dependent).

Example: Node.js ordering#

js
// node-order.js
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
Promise.resolve().then(() => console.log('promise microtask'));
process.nextTick(() => console.log('nextTick microtask'));

Typical Node output:

code
nextTick microtask
promise microtask
[ either 'immediate' or 'timeout' first, scenario-dependent ]

Why:

  • process.nextTick runs before other microtasks.
  • Promise microtasks run next.
  • Then the loop continues to a phase where either setImmediate or setTimeout fires first.

Tip: If you place the scheduling inside an I/O callback (e.g., fs.readFile), setImmediate is more likely to run before setTimeout.


Example: Browser ordering#

html
<script>
  setTimeout(() => console.log('timeout'), 0);
  Promise.resolve().then(() => console.log('promise microtask'));
  // process.nextTick / setImmediate are Node-only
</script>

Browser output:

code
promise microtask
timeout

Quick Matrix#

API / ConceptBrowser MicrotaskBrowser MacrotaskNode MicrotaskNode Macrotask
Promise.then✔️❌✔️❌
queueMicrotask✔️❌✔️❌
MutationObserver✔️❌❌ (browser only)❌
setTimeout / setInterval❌✔️❌✔️ (Timers)
requestAnimationFrame❌✔️ (render step)❌❌
setImmediate❌❌❌✔️ (Check)
process.nextTick❌❌✔️ (before other microtasks)❌

Mental Model 🧠#

  • Browser: script → microtasks (Promises) → macrotask (timeout) → render.
  • Node: phase → nextTick → Promise microtasks → next phase; setImmediate in Check, timers in Timers.

Use process.nextTick sparingly (it’s too eager), and prefer Promise microtasks for normal “run right after” semantics.


Microtask vs Macrotask — Visual Diagram

This diagram shows how a microtask (Promise) runs before a macrotask (setTimeout) after the script finishes.

Microtask vs Macrotask — Visual DiagramMicrotask vs Macrotask — Visual Diagram


That’s it — now you can reason about microtasks/macrotasks across both environments with confidence.

Filed under#javascript#event-loop#microtask#macrotask#nodejs#browser

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderNext.js App Router Caching — Cheat SheetNewer →JavaScript's Event Loop — Simple Guide (with SVG Timeline)

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

queueMicrotask Explained: The Smallest Async Queue in JavaScript

What queueMicrotask does, how it differs from Promise.then, and how microtasks really work under the hood.

Dec 31, 20251 min read1

Web Workers Explained for Frontend Developers

How Web Workers work, why they matter, and when to use them to achieve real parallelism in frontend apps.

Dec 30, 20253 min read2

JavaScript's Event Loop — Simple Guide (with SVG Timeline)

Event loop explained with a clean SVG timeline showing the actual execution order: Start → End → Promise → Timeout.

Sep 20, 20251 min read

Get new posts in your inbox

No spam. Unsubscribe any time.

3 min left
Discuss👍 React
👍