Skip to content
Async JavaScriptChapter 30 / 47Advanced4 min read

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 thinkingWith the event loop
Everything waits until the request respondsThe request runs in the background, the UI keeps working
A button click responds lateA click is handled right away
The output order makes no senseA clear order of stack, microtasks, tasks
Bugs feel randomYou can predict the order

3. Where is it used?

SituationRole of the event loop
setTimeoutThe callback goes into the task queue
PromisesCode after .then and await goes into the microtask queue
Click eventsThe event handler runs as a task
fetchThe network works in the background, the result comes through a promise
RenderingThe browser paints the screen between tasks

4. How does it work?

PartJob
Call stackThe functions running right now
Web APIsTimers, fetch, DOM events (part of the browser)
Microtask queuePromise callbacks, queueMicrotask, code after await
Task queuesetTimeout, setInterval, events (macrotasks)
One round of the event loop
Sync code runs on the call stack
The stack is empty
Run all microtasks, including new ones
The browser renders if needed
Take one task from the task queue
Start again

5. Code example

Predict the output order

js
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:

bash
1: start
5: end
3: promise
4: microtask
2: timeout

Order inside an async function

js
async function run() {
  console.log("A");
  await null;
  console.log("C");
}

run();
console.log("B");

How does the code work?

StepWhat is happening
1 and 5Sync code, goes straight onto the stack
setTimeout(..., 0)Even after 0ms it goes into the task queue and waits
.then and queueMicrotaskMicrotask queue, they run as soon as the stack is empty
2: timeoutAfter all the microtasks, last of all
await nullThe 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

Suggested order
How JavaScript Runs
Event Loop
Timers and Callbacks
Promises
Async Await

Final quick revision

PointDetail
Single threadOne job at a time on the stack
MicrotasksPromises, queueMicrotask, code after await
MacrotaskssetTimeout, setInterval, events
OrderSync, all microtasks, then one macrotask
RenderingBetween tasks, not while the stack is busy