Microtasks vs. Macrotasks
In JavaScript's Event Loop, not all asynchronous tasks are treated equally. They are divided into two queues: the Macrotask Queue (often just called the Task Queue) and the Microtask Queue.
1. The Queues
Macrotasks (Task Queue)
These are "heavy" tasks. The event loop processes them one by one.
setTimeoutsetIntervalsetImmediate(Node.js)- I/O operations
- UI Rendering
- Script loading
Microtasks
These are "lightweight" tasks that need to happen immediately after the current operation completes.
Promisecallbacks (.then,.catch,.finally)queueMicrotaskMutationObserverprocess.nextTick(Node.js - technically has its own higher priority queue, but often grouped here conceptually)
2. The Execution Rule
The Event Loop follows this specific logic:
- Execute one Macrotask from the Macrotask Queue (e.g., the initial script).
- Execute ALL Microtasks in the Microtask Queue until it is empty.
- Update the UI (Rendering).
- Repeat.
Crucial Difference:
- If a Microtask adds another Microtask, the new one is executed in the same cycle. This can block the Event Loop indefinitely if not careful.
- Macrotasks wait for the next cycle.
3. Code Example
console.log('1. Script Start');
setTimeout(() => {
console.log('2. setTimeout (Macrotask)');
}, 0);
Promise.resolve()
.then(() => {
console.log('3. Promise 1 (Microtask)');
})
.then(() => {
console.log('4. Promise 2 (Microtask)');
});
queueMicrotask(() => {
console.log('5. queueMicrotask (Microtask)');
});
console.log('6. Script End');
Output Order
1. Script Start(Synchronous)6. Script End(Synchronous)3. Promise 1 (Microtask)(Microtasks run immediately after stack clears)5. queueMicrotask (Microtask)4. Promise 2 (Microtask)(Chained promise added to microtask queue)2. setTimeout (Macrotask)(Runs in the next loop iteration)
programming/javascript/vanilla/event-loop programming/javascript/vanilla/javascript