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
- Run the current script or callback until the call stack is empty.
- Run all microtasks: promise reactions,
queueMicrotask, and the code after anawait. - In browsers, render if needed.
- 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 andawait. 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.nextTickorsetImmediate?"nextTickruns before promise microtasks, right after the current operation;setImmediateruns 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.
What should you learn before The event loop?
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
- Top 20 JavaScript interview questions with answers: core JS, Node.js and React4 questions on The event loop
- Advanced JavaScript Interview Questions3 questions on The event loop
- Advanced Node.js Interview Questions3 questions on The event loop
- Intermediate Node.js Interview Questions2 questions on The event loop
- Intermediate JavaScript Interview Questions1 question on The event loop