Event Loop
Learn what the JavaScript event loop is and how the call stack, microtask queue, and task queue work together to run async code, with output examples.
On this page
1. What is the event loop?
JavaScript is single threaded, which means it runs one thing at a time. Even so, timers, network requests, and clicks are handled side by side. This is the event loop's doing.
Long jobs (like a network call) are handed to the browser or Node.js. When they are ready, their callback goes into a queue, and the event loop brings it onto the stack.
2. Why does it matter?
If JavaScript blocked and waited for every network request, the page would freeze. The event loop makes non-blocking code possible.
| Blocking thinking | With the event loop |
|---|---|
| Everything waits until the request responds | The request runs in the background, the UI keeps working |
| A button click responds late | A click is handled right away |
| The output order makes no sense | A clear order of stack, microtasks, tasks |
| Bugs feel random | You can predict the order |
3. Where is it used?
| Situation | Role of the event loop |
|---|---|
| setTimeout | The callback goes into the task queue |
| Promises | Code after .then and await goes into the microtask queue |
| Click events | The event handler runs as a task |
| fetch | The network works in the background, the result comes through a promise |
| Rendering | The browser paints the screen between tasks |
4. How does it work?
| Part | Job |
|---|---|
| Call stack | The functions running right now |
| Web APIs | Timers, fetch, DOM events (part of the browser) |
| Microtask queue | Promise callbacks, queueMicrotask, code after await |
| Task queue | setTimeout, setInterval, events (macrotasks) |
5. Code example
Predict the output order
console.log("1: start");
setTimeout(() => console.log("2: timeout"), 0);
Promise.resolve().then(() => console.log("3: promise"));
queueMicrotask(() => console.log("4: microtask"));
console.log("5: end");Output:
1: start
5: end
3: promise
4: microtask
2: timeoutOrder inside an async function
async function run() {
console.log("A");
await null;
console.log("C");
}
run();
console.log("B");How does the code work?
| Step | What is happening |
|---|---|
1 and 5 | Sync code, goes straight onto the stack |
setTimeout(..., 0) | Even after 0ms it goes into the task queue and waits |
.then and queueMicrotask | Microtask queue, they run as soon as the stack is empty |
2: timeout | After all the microtasks, last of all |
await null | The rest of the function becomes a microtask, so the order is A B C |
Interview tip
setTimeout(fn, 0) does not mean "immediately", it means "after the stack and microtasks finish". This question often shows up as output prediction.
6. Common mistakes
Mistake: Freezing the page with a long sync loop
Why? While the stack is busy, the event loop handles neither clicks nor rendering.
Better: Break heavy work into small pieces or send it to a Web Worker.
Mistake: Thinking setTimeout 0 runs first
Why? A timer callback is a macrotask, and promise microtasks run before it.
Better: Remember the order: sync, microtasks, then a macrotask.
Mistake: Creating an infinite chain of microtasks
Why? Rendering does not happen until the microtask queue is empty, so the page hangs.
Better: For recursive work, use tasks like setTimeout or requestAnimationFrame.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | How JavaScript Runs, Functions |
| After | Timers, Callbacks, Promises, Async / Await |
Final quick revision
| Point | Detail |
|---|---|
| Single thread | One job at a time on the stack |
| Microtasks | Promises, queueMicrotask, code after await |
| Macrotasks | setTimeout, setInterval, events |
| Order | Sync, all microtasks, then one macrotask |
| Rendering | Between tasks, not while the stack is busy |