Event Loop — JavaScript

JavaScript runs one thing at a time, and the event loop decides the order. Plain code on the call stack (the place for the code that is running) runs first…

A stall has only one cashier, and that cashier serves customers one at a time. A customer whose order has to be cooked first gets a queue number, and the cashier serves the next person right away. When the food is ready, the number is called. But that customer is served only after the cashier has finished with the person who is at the counter.

JavaScript works like that cashier: it runs code one piece at a time, one thing at a time. Code that has to wait, such as a setTimeout callback, is like the order that has to be cooked first. That code is not run right away. It is put in a queue and run later. The event loop is the rule that decides when the code in the queue gets its turn.

Guess the printing order

The code below prints four letters. Before you read the explanation, guess their order:

console.log("A");

setTimeout(() => {
  console.log("B");
}, 0);

Promise.resolve().then(() => {
  console.log("C");
});

console.log("D");

What is printed: A, D, C, B. Step by step:

  1. console.log("A") runs right away.
  2. setTimeout(..., 0) does not run its callback now. That callback goes into the task queue, even though the delay is 0 milliseconds.
  3. Promise.resolve() makes a Promise, which is a kind of promise that a result will come later. Here that promise is finished right away. .then(...) stores a callback to run after the promise is finished. This callback goes into a different queue: the microtask queue. Promises are covered in full in the Fetch & Promises lesson.
  4. console.log("D") runs right away.
  5. The main code is finished. Now every microtask runs first (C), and only then one task (B).

So setTimeout(..., 0) does not mean its callback runs right away. 0 milliseconds only means "as soon as possible". As soon as possible still comes after the main code and every microtask.

The event loop sets this order, and it is always the same: plain code first, from top to bottom. Then every Promise callback. Last, the setTimeout callbacks.

Three places where code waits its turn

The code that is running and those two queues have official names:

The event loop repeats the same three steps again and again:

  1. Run the call stack until it is empty.
  2. Run every microtask until that queue is empty.
  3. Take one task and run it, then go back to step 1.

So every microtask always finishes before the next task runs, even when that task was written first in the code.

⚠️ Written order isn't running order

🔬 Try it yourself

The tool below runs a few programs step by step, including ones that use async/await. The tool marks which code runs right away, which is a microtask, and which is a macrotask (another name for a task).

TL;DR: JavaScript runs one thing at a time, and the event loop decides the order. Plain code on the call stack (the place for the code that is running) runs first. Then every microtask: .then callbacks from Promises and the code after await (both covered later). After that comes one macrotask, such as a setTimeout callback or a click. This order repeats again and again. That is why a setTimeout(fn, 0) callback still runs after a Promise callback, and why a heavy loop freezes the page.

⚡ Written order isn't running order

Write:

console.log("A");

setTimeout(() => {
  console.log("B");
}, 0);

Promise.resolve().then(() => {
  console.log("C");
});

console.log("D");
Guess first: setTimeout and .then(...) both store a callback to run later. In what order are the letters printed?

A, D, C, B. Both callbacks go into a queue, and they run only after the main code has finished.

Meaning: setTimeout(..., 0) does not mean its callback runs right away. 0 milliseconds only means "as soon as possible", and that still comes after the main code has finished.

⚡ Microtasks first, then a task

Meaning: The event loop is the rule that decides which code gets its turn. The event loop repeats three steps: run the plain code until it is finished, run every microtask (.then callbacks from Promises, and the code after await, covered later), then take one task (setTimeout callbacks, clicks). That is why C is printed before B.

⚡ A heavy loop freezes the page

Meaning: JavaScript runs one thing at a time. While the running code (a long loop, for example) has not finished, clicks and screen updates wait too. Split heavy work into small pieces.

What you will learn