0
0
Node.jsframework~10 mins

Event loop mental model in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event loop mental model
Start: Node.js program runs
Call Stack: Execute sync code
Encounter async task?
NoContinue sync
Yes
Register callback in Task Queue
Event Loop checks Call Stack empty?
No
Move callback from Task Queue to Call Stack
Execute callback
Repeat Event Loop
The event loop runs continuously, executing synchronous code first, then processing asynchronous callbacks from the task queue when the call stack is empty.
Execution Sample
Node.js
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
This code logs 'Start', schedules a timeout callback, then logs 'End'. The timeout callback runs after synchronous code.
Execution Table
StepCall StackTask QueueActionOutput
1console.log('Start')[]Execute sync logStart
2setTimeout(callback, 0)[callback]Register callback in Task QueueNo output
3console.log('End')[callback]Execute sync logEnd
4empty[callback]Event Loop moves callback to Call StackNo output
5callback executionemptyExecute callback logTimeout
6emptyemptyNo more tasks, program endsNo output
💡 All synchronous code executed and task queue emptied, event loop stops.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Call Stackemptyemptyemptycallback executionemptyempty
Task Queueemptycallback addedcallback waitingemptyemptyempty
OutputemptyStart printedStart and End printedStart and End printedStart, End and Timeout printedAll printed
Key Moments - 2 Insights
Why does 'Timeout' print after 'End' even though setTimeout delay is 0?
Because setTimeout callback is placed in the task queue and only runs after the call stack is empty, so synchronous code like 'End' logs first (see execution_table steps 2-5).
What happens if the call stack is not empty when the event loop checks the task queue?
The event loop waits until the call stack is empty before moving callbacks from the task queue to the call stack (see concept_flow and execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is in the call stack at Step 3?
AsetTimeout call
Bconsole.log('End')
Ccallback function
Dempty
💡 Hint
Check the 'Call Stack' column at Step 3 in the execution_table.
At which step does the event loop move the callback from the task queue to the call stack?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look for the action 'Event Loop moves callback to Call Stack' in the execution_table.
If the setTimeout delay was increased to 1000ms, how would the execution table change?
ACallback would stay longer in task queue before Step 4
BCallback would move to call stack immediately at Step 4
CCallback would execute before 'End' logs
DNo change in execution order
💡 Hint
Consider how setTimeout delay affects when callback enters the task queue and event loop timing.
Concept Snapshot
Event Loop in Node.js:
- Runs synchronous code first on Call Stack
- Async callbacks go to Task Queue
- Event Loop moves callbacks to Call Stack when empty
- setTimeout with 0 delay still runs after sync code
- Keeps Node.js non-blocking and responsive
Full Transcript
The Node.js event loop runs synchronous code first by placing it on the call stack. When it encounters asynchronous tasks like setTimeout, it registers their callbacks in the task queue. The event loop waits for the call stack to be empty before moving callbacks from the task queue to the call stack to execute them. This means even a setTimeout with zero delay runs after all synchronous code finishes. This model allows Node.js to handle many tasks efficiently without blocking the program.