Skip to content

event loop

The event loop is the mechanism that lets single-threaded JavaScript handle asynchronous work by taking queued jobs one at a time and running each one to completion.

JavaScript code runs on a single call stack. When a timer fires, a network response arrives, or a promise with a handler settles, the host environment, such as a browser or Node.js, queues a job, usually a callback. The event loop starts that job only after the call stack is empty, so no job interrupts another. A long synchronous job delays every job waiting behind it.

ECMA-262 defines jobs and requires each one to run to completion, but it doesn’t define the event loop itself. Browsers define it in the WHATWG HTML Standard, and Node.js implements its own loop with the libuv library.

Hosts don’t treat all jobs alike. In browsers and Node.js, promise reactions run as microtasks, which the loop runs until none are left before it moves on to the next timer or event callback.

Example

Say a checkout script schedules an analytics ping with a zero-millisecond setTimeout() timer and updates the cart in a promise callback. The program shows the order in which the event loop runs them:

Language: JavaScript Filename: checkout.mjs
console.log("Start checkout");

setTimeout(() => {
  console.log("Send analytics ping");
}, 0);

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

console.log("End checkout");

Running the module prints both synchronous lines first:

Language: Shell
$ node checkout.mjs
Start checkout
End checkout
Update cart
Send analytics ping

The module’s top-level code runs to completion first, so neither callback starts until it ends. The promise callback is a microtask, so it runs before the timer callback, even though the code scheduled the timer first.

A zero-millisecond delay sets the earliest time the timer callback can run. This program keeps the call stack busy for 200 milliseconds after scheduling a timer:

Language: JavaScript Filename: busy.mjs
const start = Date.now();

setTimeout(() => {
  const waited = Date.now() - start;
  console.log(`Waited at least 200 ms: ${waited >= 200}`);
}, 0);

while (Date.now() - start < 200) {
  // Keep the call stack busy
}

console.log("Loop finished");

The timer callback waits for the loop to finish:

Language: Shell
$ node busy.mjs
Loop finished
Waited at least 200 ms: true

The timer expired almost at once, but the event loop couldn’t start its callback while the while loop still held the call stack.

Step through either program to watch each job move from a queue onto the call stack only after the stack is empty:

Interactive diagram — enable JavaScript to view.
A developer feeds task cards into a machine with a NOW chute, a LOOP dial, and a WORKER booth where a small robot processes cards, under a large timer clock.

Course

The JavaScript Event Loop, Concurrency, and Workers

Build a weather-station dashboard in JavaScript, and learn how the event loop orders timers and promises, how to cancel work with AbortController, and when to move CPU work to a worker.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 16, 2026