Module 02
Event Loop (Perfect Deep Understanding)
Master the heartbeat of Node.js. Understand the mechanism that bridges synchronous V8 and asynchronous Libuv.
01
What is Event Loop?
Definition
The Event Loop is a mechanism that manages execution of asynchronous callbacks in Node.js.
Simple Idea
It decides "what runs next"
02
Why Event Loop Exists
Because:
- JavaScript is single-threaded
- But we still need async behavior
So:
Event Loop bridges V8 (sync) and Libuv (async)
03
High-Level Flow
1Execute sync code (Call Stack)
2Async task → sent to Libuv
3Completed task → Callback Queue
4Event Loop → pushes callback to stack
04
Key Components
1. Call Stack
Executes synchronous code
2. Web APIs / Libuv
Handles async tasks
3. Callback Queue
Stores completed async callbacks
4. Event Loop
Moves tasks → stack
05
Example (Step-by-Step)
app.js
javascript
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
console.log("End");Execution Flow
- "Start" → printed
- setTimeout → sent to Libuv
- "End" → printed
- Callback → executed later
Output
Start
End
Timeout
End
Timeout
06
Microtasks vs Macrotasks (IMPORTANT)
Microtasks (Higher Priority)
- • Promise.then()
- • queueMicrotask()
Macrotasks (Lower Priority)
- • setTimeout
- • setInterval
- • setImmediate
Rule
Microtasks run before macrotasks
Example
priority.js
javascript
console.log("Start");
setTimeout(() => console.log("Timeout"), 0);
Promise.resolve().then(() => console.log("Promise"));
console.log("End");Output
Start
End
Promise
Timeout
End
Promise
Timeout
07
Event Loop Phases (Advanced)
- 01. Timers (setTimeout)
- 02. I/O callbacks
- 03. Idle/prepare
- 04. Poll
- 05. Check (setImmediate)
- 06. Close callbacks
Important Insight
Each phase has its own queue
08
Interview Questions (Event Loop)
Theory Questions
What is Event Loop?▼
Answer:A mechanism that manages async execution by moving callbacks from queue to call stack.
Why is an Event Loop needed?▼
Answer:Because JavaScript is single-threaded but needs async behavior.
Execution Order?▼
Answer:Sync → Microtasks → Macrotasks
Why setTimeout(..., 0) is delayed?▼
Answer:Because it goes to the macrotask queue and waits for current execution and all microtasks to finish.
Microtask vs Macrotask?▼
| Microtask | Macrotask |
|---|---|
| Promise.then | setTimeout |
| Higher priority (Executes first) | Lower priority (Executes after) |
Coding Questions
Q1: The Ultimate Order Challenge
This is the most common senior-level Node.js interview question. What is the exact expected output order of this script?
index.js
javascript
console.log("1");
setTimeout(() => console.log("2"), 0);
setImmediate(() => console.log("3"));
process.nextTick(() => console.log("4"));
Promise.resolve().then(() => console.log("5"));
console.log("6");Output:
1 (Sync code runs first)
6 (Sync code finishes)
4 (nextTick queue processed)
5 (Promise resolving queue)
2 (Timer phase macrotask)
...? (Can be 2 then 3, or 3 then 2)
Note on Timers vs Immediate: When not in an I/O cycle, the order of setTimeout(0) vs setImmediate relies entirely on system performance ticks. It is non-deterministic.
Final Combined Mental Model
V8Executes JS (Call Stack)
LibuvHandles async work
Event LoopMoves async results back to execution
QueuesDecide priority