Sub topic 4
The Task Queue
This chapter assumes you know JavaScript functions and callbacks, and that you have read the earlier chapters on the call stack and host APIs. You saw there that a host API, such as a timer or a network request, does its waiting outside the JavaScript engine. This chapter covers what happens when that work finishes and its callback needs to run.
What the Task Queue Is
The task queue is a list of callbacks waiting to run. Each entry is called a task, and older material calls it a macrotask. When a host API finishes its work, it does not call your callback immediately. It places a task on the queue, and the task waits there until the engine is free.
These things commonly create tasks:
- Timer callbacks from
setTimeoutandsetInterval - UI events such as
click,keydown, andinput - I/O completions, such as a finished file read in Node.js
- Messages from
postMessage, Web Workers, andMessageChannel
The queue is first in, first out. A task runs only when the call stack is completely empty, and it runs to completion before the next task starts.
Tasks Wait for the Stack
Synchronous code always finishes before any queued task runs, even if the task was queued first:
console.log('start');
setTimeout(() => console.log('timeout 1'), 0);
setTimeout(() => console.log('timeout 2'), 0);
console.log('end');
// Output: start, end, timeout 1, timeout 2Both timers expire almost immediately, so their tasks are queued while the script is still running. They have to wait until the script returns and the stack empties. After that they run in the order they were queued.
New Tasks Go to the Back
A task that schedules another task does not jump the line. The new task joins the end of the queue:
setTimeout(() => {
console.log('task A');
setTimeout(() => console.log('task C (queued by A)'), 0);
}, 0);
setTimeout(() => console.log('task B'), 0);
// Output: task A, task B, task C (queued by A)Task B was already waiting when task A ran, so B goes before C.
More Than One Queue
The HTML specification actually defines several task queues, grouped by task source. Timers, user input, and networking can each have their own queue. Browsers keep order within one source, but they can choose which source to serve next. Many browsers favor input events so the page stays responsive. Do not assume a click and a timer will run in a fixed order relative to each other.
Common Errors
| Mistaken belief | What actually happens |
|---|---|
| A zero-delay timer runs right away | It runs after the current synchronous code and any tasks queued ahead of it |
| A long task can be interrupted by a click | The click waits in the queue until the long task finishes |
| Promise callbacks are tasks | Promise callbacks use a separate, higher-priority queue, covered in a later chapter |
Exercise
What does this code print, and in what order? setTimeout(() => console.log('X'), 0); console.log('Y'); setTimeout(() => { console.log('Z'); setTimeout(() => console.log('W'), 0); }, 0);
Show answer
Y, X, Z, W. 'Y' is synchronous, so it prints first. The tasks for X and Z are queued in that order. W is queued while Z runs, so it goes to the back of the queue and runs last.