← All posts

JavaScript Event Loop Explained with Interactive Examples

· 6 min read · JavaScript · Event Loop · Async · Browser Runtime

One thread, many callbacks

JavaScript runs your code on one thread with one call stack. It still handles timers, network responses, and clicks without blocking, because those callbacks wait in queues until the stack is free. The event loop decides which callback runs next.

Once you know its rule, you can predict the output of an async snippet when you know when its callbacks get queued, as with the fixed timer delays in the examples below. You cannot predict the order of external events, such as which of two network responses arrives first.

When the call stack is empty, the event loop runs every microtask in the microtask queue. Then it runs one task from the task queue. Then it repeats.

Three structures decide the order

The event loop moves callbacks between three structures:

  • Call stack. The stack holds the function that is running now and the functions that called it. Synchronous code runs here. The last function pushed is the first to finish (LIFO).
  • Task queue. The queue holds callbacks from setTimeout, setInterval, DOM events, and I/O. Some articles call it the macrotask queue. Callbacks from the same source, such as two timers, run in the order they were queued (FIFO). The browser can pick callbacks from different sources in a different order.
  • Microtask queue. The queue holds callbacks from Promise.then, Promise.catch, Promise.finally, queueMicrotask, and MutationObserver. It is also FIFO.

A microtask is not faster than a task. It has higher priority. The event loop empties the whole microtask queue before it takes a single task, and that includes microtasks queued while the queue drains.

Example 1: sync code, then promises, then timeouts

The script logs A and D directly. It queues B with setTimeout(..., 0) and C with Promise.resolve().then(...).

Predict the output, then step through the demo.

Interactive Event Loop Demo

Simple Example

Synchronous logs vs Promise microtask vs setTimeout task

Code

console.log('A');

setTimeout(() => {
  console.log('B - timeout');
}, 0);

Promise.resolve().then(() => {
  console.log('C - promise');
});

console.log('D');

Output Challenge

Test Yourself

Write the console output in order, one line per entry.

Step 1 / 8: Script starts on call stack

Global script enters the call stack.

Call Stack (Top First)

global()

Microtask Queue (Front First)

No microtasks waiting

Task Queue (Front First)

No tasks waiting

Console Output So Far

No console output yet.

The output is A, D, C - promise, B - timeout. Both synchronous logs run while the script is still on the stack. When the script ends, the microtask C runs, and only then does the event loop take the timeout B from the task queue. A delay of 0 does not mean "now". It means "in a later task".

Example 2: callbacks that queue more callbacks

This script nests a microtask inside a timeout, and a timeout inside a microtask.

Interactive Event Loop Demo

Complex Example

Nested microtasks and tasks with ordering across turns

Code

console.log('1');

setTimeout(() => {
  console.log('2 - timeout 1');

  Promise.resolve().then(() => {
    console.log('3 - microtask inside timeout');
  });
}, 0);

Promise.resolve().then(() => {
  console.log('4 - microtask 1');

  setTimeout(() => {
    console.log('5 - timeout inside microtask');
  }, 0);
});

console.log('6');

Output Challenge

Test Yourself

Write the console output in order, one line per entry.

Step 1 / 10: Script enters stack

Global script begins.

Call Stack (Top First)

global()

Microtask Queue (Front First)

No microtasks waiting

Task Queue (Front First)

No tasks waiting

Console Output So Far

No console output yet.

The output is 1, 6, 4, 2, 3, 5:

  1. The synchronous logs 1 and 6 run first.
  2. The stack is empty, so microtask 4 runs. It queues timeout 5 behind timeout 2, which is already waiting.
  3. The event loop takes timeout 2. Inside it, the promise queues microtask 3.
  4. Timeout 2 finishes and the stack is empty again. Microtask 3 runs before the next task.
  5. The event loop takes timeout 5.

Step 4 is the one people get wrong. A microtask queued inside a task runs right after that task, before any other task, even one that was queued earlier.

Example 3: a timeout delay is a minimum

The script queues a 1000 ms timeout and then a 0 ms timeout. It also queues a microtask and logs Sync end.

Interactive Event Loop Demo

Timeout Not Zero Example

Why setTimeout with 1000ms is a minimum delay, not exact timing

Code

const start = Date.now();

setTimeout(() => {
  console.log('T1 ~1000ms', Date.now() - start);
}, 1000);

setTimeout(() => {
  console.log('T2 ~0ms', Date.now() - start);
}, 0);

Promise.resolve().then(() => {
  console.log('Microtask', Date.now() - start);
});

console.log('Sync end', Date.now() - start);

Output Challenge

Test Yourself

Write the console output in order, one line per entry.

Step 1 / 9: Script starts

Global script enters stack and sets reference start time.

Call Stack (Top First)

global()

Microtask Queue (Front First)

No microtasks waiting

Task Queue (Front First)

No tasks waiting

Console Output So Far

No console output yet.

The output is Sync end, Microtask, T2 ~0ms, T1 ~1000ms. The script starts the 1000 ms timer first, but its callback enters the task queue only after 1000 ms pass, so T2 runs before it.

T1 never runs before 1000 ms have passed. The timer puts the callback in the task queue after the delay. (The number it logs comes from Date.now(), which reads the system clock, so a clock adjustment can make it read lower.) The callback runs when the stack is empty and the tasks ahead of it have finished. If synchronous code keeps the stack busy for two seconds, T1 runs after two seconds.

What to check when you predict output

  • Synchronous code runs first, top to bottom, on the call stack.
  • When the stack is empty, every queued microtask runs before the next task.
  • A microtask queued during a task runs before the task after it.
  • setTimeout(..., 0) waits for a later task.
  • A timeout delay is the shortest wait, not an exact time.