Sub topic 7
The Microtask Queue
This chapter assumes you know how the call stack works, that host APIs hand finished work back as tasks, and that the event loop runs one task per turn. You should also be comfortable with basic Promise.then usage.
A Second, Higher-Priority Queue
The task queue is not the only queue in the event loop. JavaScript engines also keep a microtask queue. Microtasks are short callbacks that must run as soon as the current piece of code finishes. They do not wait for the next turn of the loop.
The microtask queue follows one rule: once the call stack is empty, the engine drains the microtask queue completely before it does anything else. "Drain" means the engine runs microtasks one after another until none are left. It does this before it takes the next task, and in browsers before it renders.
Promise reactions are the most common source of microtasks. A later chapter covers them in detail. Here they serve as an easy way to put a callback in the queue.
When the Queue Is Drained
A microtask checkpoint is the point where the engine drains the queue. It happens in these places:
- After the initial script finishes running.
- After every single task, such as a timer callback, an event handler, or an I/O callback.
- In browsers, after each callback run during the rendering steps.
Because draining happens after every task, microtasks queued by a task always run before the next task starts.
setTimeout(() => {
console.log('task 1');
Promise.resolve().then(() => console.log('microtask from task 1'));
}, 0);
setTimeout(() => console.log('task 2'), 0);
// Output: task 1, microtask from task 1, task 2Both timers are ready at the same moment, but the microtask queued inside task 1 runs before task 2.
Microtasks That Queue Microtasks
The drain loop keeps going while the queue still has entries. A microtask that schedules another microtask adds it to the end of the same queue, so the new one also runs before the next task.
setTimeout(() => console.log('task'), 0);
Promise.resolve().then(() => {
console.log('micro 1');
Promise.resolve().then(() => console.log('micro 3'));
});
Promise.resolve().then(() => console.log('micro 2'));
console.log('sync');
// Output: sync, micro 1, micro 2, micro 3, taskStep by step:
- The script queues a timer task and two microtasks (
micro 1,micro 2), then logssync. - The stack empties, so the drain begins.
micro 1runs and appendsmicro 3. micro 2runs, thenmicro 3.- The queue is now empty, so the timer task finally runs.
Tasks vs. Microtasks
| Property | Task queue | Microtask queue |
|---|---|---|
| Typical sources | Timers, events, I/O | Promise reactions, `queueMicrotask` |
| How many run per turn | One | All of them, including newly added ones |
| Runs before rendering | Not guaranteed | Yes, always |
Exercise
What does this code print, and in what order? setTimeout(() => console.log('A'), 0); Promise.resolve().then(() => { console.log('B'); setTimeout(() => console.log('C'), 0); Promise.resolve().then(() => console.log('D')); }); console.log('E');
Show answer
E, B, D, A, C. The synchronous log E runs first. The drain runs B, which queues the timer C behind A and adds the microtask D, which runs in the same drain. After that, the tasks A and C run in the order they were queued.
Exercise
A click handler resolves a promise whose .then callback updates a variable. Can a setTimeout(fn, 0) callback that was already queued before the click run between the handler and that .then callback?
Show answer
No. The click handler is a task. When it finishes, the microtask queue is drained, so the .then callback runs before the event loop picks up any other task, including the pending timer.