Sub topic 5
One Turn of the Event Loop
This chapter assumes you know how the call stack works, that host APIs such as timers do their waiting outside the JavaScript engine, and that finished host work places callbacks in the task queue. Here we connect those pieces into one repeating cycle: the event loop.
The Loop in Plain Terms
The event loop is a simple algorithm that the host (the browser or Node.js) runs over and over. Each pass through it is called a turn, or an iteration. In its simplest form, a turn looks like this:
- Take the oldest task from the task queue.
- Run it on the call stack until the stack is empty.
- Process the microtask queue. The Microtask Queue chapter covers this step.
- In a browser, update the rendering if needed.
- Go back to step 1. If no task is waiting, sleep until one arrives.
The loop never interrupts a running task. It only picks the next task once the call stack is empty. This is run-to-completion applied to the whole program.
The Script Itself Is a Task
When a page loads a <script> or Node.js runs a file, the whole top-level script runs as the first task. Callbacks you schedule during that script cannot run until the script finishes and the stack empties.
console.log('script start');
setTimeout(() => {
console.log('task 1');
setTimeout(() => console.log('task 3'), 0);
}, 0);
setTimeout(() => console.log('task 2'), 0);
console.log('script end');
// Output: script start, script end, task 1, task 2, task 3Here is how the turns play out:
| Turn | Task run | Queue afterward |
|---|---|---|
| 1 | Main script: logs two lines, schedules two timers | task 1, task 2 |
| 2 | task 1: logs, schedules task 3 | task 2, task 3 |
| 3 | task 2: logs | task 3 |
| 4 | task 3: logs | empty |
Task 3 runs after task 2 because it joined the queue later. A task scheduled from inside another task always waits for the tasks already queued.
Common Mistakes
- Expecting a callback to run as soon as its timer expires. The timer only queues the task. The task waits until the stack is empty and every earlier task has run.
- Thinking the loop runs alongside your code. The loop and your code share one thread. While your code runs, the loop is not turning.
- Treating "the queue" as a single list. Browsers keep several task sources, such as timers, user input, and network events, and may favor some sources over others. Within a single source, order is preserved.
Exercise
A click handler runs for 50 ms. During that time a 10 ms timer expires and the user clicks a second time. When can either callback start, and what is still running until then?
Show answer
Neither callback can start until the first click handler returns and the call stack is empty. Both callbacks sit in task queues until then. After that, the loop picks them in later turns, one task per turn, each running to completion.
Exercise
Predict the output: setTimeout(() => console.log('x'), 0); for (let i = 0; i < 3; i++) console.log(i); setTimeout(() => console.log('y'), 0);
Show answer
0, 1, 2, x, y. The loop is synchronous and finishes within the script task. Then the two timer tasks run in the order they were queued.