BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

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

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Posts/javascript
Intermediate1 min read

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.

Mohsen Fallahnejad

Mohsen Fallahnejad

Dec 31, 20251 readsDiscuss

queueMicrotask Explained

queueMicrotask is a low-level JavaScript API that lets you schedule a function to run in the microtask queue.

It runs:

  • After the current synchronous code
  • Before any macrotasks (setTimeout, setInterval, requestAnimationFrame)
  • Before rendering

🧠 What Is a Microtask?#

JavaScript has two main async queues:

QueueExamples
MicrotaskqueueMicrotask, Promise.then, await
MacrotasksetTimeout, events

JavaScript always drains the microtask queue fully before moving on.


⚙️ Basic Usage#

js
queueMicrotask(() => {
  console.log("microtask");
});

console.log("sync");

Output:

code
sync
microtask

🔍 queueMicrotask vs Promise.then#

FeaturequeueMicrotaskPromise.then
Creates Promise❌✅
Intent clarity✅⚠️
Error handlingGlobalPromise-based

⚠️ Error Behavior#

js
queueMicrotask(() => {
  throw new Error("boom");
});

This becomes a global error, not a rejected Promise.


🔁 Execution Order Example#

js
console.log("start");

setTimeout(() => console.log("timeout"), 0);
queueMicrotask(() => console.log("microtask"));
Promise.resolve().then(() => console.log("promise"));

console.log("end");

Output:

  1. 1

    start

  2. 2

    end

  3. 3

    microtask

  4. 4

    promise

  5. 5

    timeout

then it depends
Sync code runs first
Microtasks drain next
Macrotasks (setTimeout) run last

🚨 Microtask Starvation#

js
function loop() {
  queueMicrotask(loop);
}
loop();

This freezes the page because microtasks never finish.


🧩 When to Use#

✅ Precise scheduling
✅ Libraries / frameworks
❌ Delays or animations


✅ Summary#

ConceptMeaning
queueMicrotaskSchedules microtask
PriorityHigher than timers
Use carefullyYes

✨ Final Takeaway#

queueMicrotask is powerful but sharp — use it only when you need microtask precision.

Filed under#javascript#event loop#microtask#async

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 ↗
← OlderWhy async/await Doesn’t Make JavaScript Code ParallelNewer →CDN Explained: What It Is, How It Works, and How to Use It in Next.js

Start the discussion

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

Read next

Why async/await Doesn’t Make JavaScript Code Parallel

Async/await feels like parallel code, but it isn’t. Here’s why JavaScript is still single-threaded and how async really works.

Dec 30, 20253 min read1

How Promise.all Works in a Single-Threaded JavaScript World

If JavaScript is single-threaded, how can Promise.all handle multiple requests at the same time? A deep dive into the event loop and async I/O.

Dec 30, 20252 min read1

Promise APIs Compared: all vs race vs any vs allSettled

A practical and conceptual comparison of Promise.all, Promise.race, Promise.any, and Promise.allSettled.

Dec 30, 20253 min read2

Get new posts in your inbox

No spam. Unsubscribe any time.

1 min left
Discuss👍 React
👍