0
0
Node.jsframework~10 mins

Event loop phases and timer execution in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event loop phases and timer execution
Start Event Loop
Timers Phase
Pending Callbacks Phase
Idle, Prepare Phase
Poll Phase
Check Phase
Close Callbacks Phase
Repeat Event Loop
The Node.js event loop runs in phases, each handling specific callbacks like timers, I/O, and close events, cycling continuously.
Execution Sample
Node.js
setTimeout(() => console.log('timer1'), 0);
setImmediate(() => console.log('immediate1'));
console.log('start');
This code schedules a timer and an immediate callback, then logs 'start' immediately.
Execution Table
StepPhaseActionCallback ExecutedOutput
1Main scriptExecute synchronous codeNonestart
2Timers phaseExecute timer callbackstimer1 callbacktimer1
3Pending CallbacksNo callbacksNone
4Idle, PrepareInternal setupNone
5Poll phaseCheck for I/O callbacksNone
6Check phaseExecute immediate callbacksimmediate1 callbackimmediate1
7Close callbacksNo callbacksNone
8Event loop repeatsCycle continuesDepends on new events
💡 Event loop runs continuously until no more callbacks or process exit
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
Timers queueemptytimer1 scheduledtimer1 executedemptyempty
Immediates queueemptyimmediate1 scheduledimmediate1 pendingimmediate1 executedempty
Console outputstartstart, timer1start, timer1, immediate1start, timer1, immediate1
Key Moments - 3 Insights
Why does 'start' print before 'timer1' even though timer1 has 0 delay?
'start' is logged during the main script execution (Step 1) before the event loop phases begin. Timer callbacks run only in the Timers phase (Step 2), so they come after synchronous code.
Why does 'immediate1' print after 'timer1' even though setImmediate is called after setTimeout?
setTimeout callbacks run in the Timers phase (Step 2), while setImmediate callbacks run in the Check phase (Step 6). The event loop processes Timers phase before Check phase, so timer1 prints first.
Can the order of timer and immediate callbacks change?
Yes, depending on when they are scheduled and the current phase of the event loop. But in this example, timers run before immediates consistently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at Step 1?
A"immediate1"
B"timer1"
C"start"
DNothing
💡 Hint
Check the 'Output' column for Step 1 in the execution_table
At which step does the 'immediate1' callback execute?
AStep 6
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'immediate1 callback' in the 'Callback Executed' column
If setTimeout delay was increased, how would that affect the execution order?
Atimer1 would run before immediate1
Bimmediate1 would run before timer1
CBoth run at the same time
DNeither would run
💡 Hint
Consider the event loop phases and when timers vs immediates run
Concept Snapshot
Node.js event loop cycles through phases:
Timers (setTimeout/setInterval callbacks)
Pending Callbacks (I/O callbacks)
Idle, Prepare (internal)
Poll (I/O polling)
Check (setImmediate callbacks)
Close Callbacks
Timers run before immediates
Synchronous code runs before event loop phases
Full Transcript
The Node.js event loop runs continuously in phases. First, synchronous code runs immediately. Then the Timers phase executes callbacks scheduled by setTimeout or setInterval. Next phases handle I/O callbacks and internal tasks. The Check phase runs callbacks scheduled by setImmediate. This cycle repeats until no callbacks remain. In the example, 'start' logs first during synchronous execution. Then the timer callback runs in the Timers phase, printing 'timer1'. Finally, the immediate callback runs in the Check phase, printing 'immediate1'. This order is consistent because timers run before immediates in the event loop phases.