JavaScript Reference ยท Promises and Async
The event loop
Reference for the JavaScript event loop: the call stack, task queue and microtask queue, why promises run before timers, and how long scripts block the page.
JavaScript runs your code on a single thread, one piece at a time. The event loop is the mechanism that decides what runs next: it picks work from queues, runs it until the call stack is empty, then picks again. Timers, user input, network responses and promise callbacks all reach your code through it.
Understanding the order in which queued work runs explains most surprises with setTimeout(), promises and async functions. The details of rendering differ between browsers and Node.js, but the core rules below hold in both.
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The pieces
- Call stack. The functions currently running. Your script is one big task on it; each call pushes a frame, each return pops one.
- Task queue (macrotasks). Callbacks for timers, DOM events, message events and similar sources. The loop takes one task, runs it to completion, and moves on.
- Microtask queue. Promise reaction callbacks (
then,catch,finally, and the continuation afterawait) and callbacks fromqueueMicrotask(). After each task, and whenever the call stack empties, the loop drains the microtask queue completely before anything else. - Rendering (browsers). Between tasks the browser may update the screen.
requestAnimationFrame()callbacks run just before a paint.
The order of things
- The current script (or callback) runs to completion. Nothing interrupts it.
- All microtasks run, including microtasks queued by other microtasks.
- The browser may render.
- The next task runs, and the cycle repeats.
That is why, in the first example, the timeout with a delay of 0 prints last: it is a task, and the promise callbacks are microtasks that run first. A delay of 0 means "as soon as possible, after the current work", not "now".
Blocking the loop
Because tasks run to completion, a long synchronous computation delays everything else: timers fire late, clicks are not handled and the page does not repaint.
setTimeout(() => console.log("timer"), 0); // Blocks for as long as the loop takes. The timer waits. for (let i = 0; i < 1e9; i++) {} console.log("loop finished");
To keep a page responsive, split long work into chunks scheduled with setTimeout() or requestIdleCallback(), or move it to a Web Worker.
Starving works the other way, too: a microtask that keeps queueing another microtask forever never lets the loop reach the next task or a repaint.
Usage notes
- Timers are minimum delays, not promises. The callback runs when the delay has passed and the loop is free.
- Order between timers with different delays follows the delay (and registration order for equal delays), not code position.
awaitalways yields at least one microtask turn, even for non-promise values.- Node.js has the same microtask rule but a more detailed task model (timers, I/O,
setImmediate()phases) andprocess.nextTick(), which runs even before promise microtasks. - Web Workers have their own event loop and communicate through messages.
- Errors in a task do not stop the loop; the next task still runs.
Browser support
The event loop and microtask queue are part of every JavaScript environment, and queueMicrotask() is available in all current browsers and Node.js.