learn deep. ship safe.Practice testsFollow ByteMunSubscribe

Concept

The event loop

How a single-threaded runtime runs callbacks from the task and microtask queues.

The event loop explained

The event loop is how JavaScript runs asynchronous code on a single thread. The engine runs one piece of code at a time on the call stack; when the stack is empty, the event loop first runs every queued microtask (such as promise callbacks), then takes the next task (such as a timer or an I/O callback), and repeats. That order explains why setTimeout(fn, 0) never runs before a resolved promise's .then.

How the event loop decides what runs next

  1. Run the current script or callback until the call stack is empty.
  2. Run all microtasks: promise reactions, queueMicrotask, and the code after an await.
  3. In browsers, render if needed.
  4. Take one task (macrotask): a timer, a network or file callback, a user event. Go back to step 1.
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// Output: A, D, C, B

A and D run first because they are on the stack. C is a microtask, so it runs as soon as the stack is empty. B is a task and waits for the next turn of the loop.

The event loop in Node.js

Node.js uses libuv, which splits the loop into phases: timers, pending callbacks, poll (I/O), check (setImmediate) and close callbacks. Between phases Node empties two queues: process.nextTick callbacks first, then promise microtasks. Heavy CPU work blocks every phase, which is why it belongs in worker threads.

Common mistakes

  • Blocking the loop with a long synchronous loop or a huge JSON.parse: every request and timer waits behind it.
  • Assuming setTimeout(fn, 0) means "now". It means "after the current code and all microtasks", and browsers clamp nested timers.
  • Starving the loop with microtasks: a promise chain that keeps scheduling new microtasks prevents timers and rendering from ever running.

What interviewers ask about the event loop

  • "Predict the output" of a mix of console.log, setTimeout, promises and await. Walk through stack, microtasks, then tasks.
  • "What is the difference between microtasks and macrotasks?" Microtasks all run before the next task; tasks run one per loop turn.
  • "process.nextTick or setImmediate?" nextTick runs before promise microtasks, right after the current operation; setImmediate runs in the check phase after I/O.
  • "How do you keep a Node.js server responsive under CPU load?" Move the work to worker threads or another process.
Concept map
Learn firstCallbacks
Part of JavaScriptThe event loop

What should you learn before The event loop?

  1. FunctionsReusable blocks of code; in JavaScript they are values that can be passed around and returned.
  2. CallbacksFunctions passed to other functions to be called later, the original way to handle async work.

What to learn after The event loop

  • Async/awaitKnowing the microtask queue explains when await resumes.
  • StreamsProcessing data piece by piece instead of loading it all into memory, with backpressure.
  • Worker threadsWorker threads exist to keep the event loop free.

Connections to other concepts

Part of
JavaScript

Practise The event loop

All topics