Chapter XVI · Asynchronous JavaScript
Microtask Queue
The microtask queue holds high-priority asynchronous tasks like Promise `.then()` callbacks and `queueMicrotask`.
01 How It Works
Think of express emergency medical triage patients who jump ahead of regular waiting room patients.
After every call stack clearance, the event loop drains the *entire* microtask queue before moving on to macrotasks or rendering.
example.js
Promise.resolve().then(() => console.log("Microtask 1"));
1. Promise resolves or `queueMicrotask` is called.
2. Callback is placed in the microtask queue.
3. Event loop executes all microtasks fully before any macrotask.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
queueMicrotask(() => console.log("Express microtask"));
Key Takeaway
Microtasks have higher priority than macrotasks and run immediately after the call stack empties.