Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the event loop in Node.js?
The event loop is a process that handles asynchronous callbacks in Node.js. It allows Node.js to perform non-blocking operations by offloading tasks and executing callbacks when ready.
Click to reveal answer
intermediate
Name the main phases of the Node.js event loop.
The main phases are: timers, pending callbacks, idle/prepare, poll, check, and close callbacks. Each phase has a specific role in processing callbacks and events.
Click to reveal answer
beginner
What happens during the 'timers' phase of the event loop?
During the timers phase, callbacks scheduled by setTimeout() and setInterval() are executed if their time has expired.
Click to reveal answer
intermediate
How does the 'poll' phase work in the event loop?
The poll phase waits for new I/O events and executes their callbacks. If there are no timers to run, it will wait for callbacks or new events to arrive.
Click to reveal answer
intermediate
Why might a timer callback be delayed in Node.js?
Timer callbacks can be delayed if the event loop is busy running other callbacks or if the poll phase is waiting for I/O. Timers only run after their delay and when the event loop reaches the timers phase.
Click to reveal answer
Which event loop phase executes callbacks from setTimeout()?
Acheck
Bpoll
Ctimers
Dclose callbacks
✗ Incorrect
The timers phase runs callbacks scheduled by setTimeout() and setInterval().
What does the poll phase do in the Node.js event loop?
AExecutes close callbacks
BExecutes setImmediate callbacks
CRuns timer callbacks
DWaits for and processes I/O events
✗ Incorrect
The poll phase waits for new I/O events and executes their callbacks.
When are setImmediate() callbacks executed?
ADuring the check phase
BDuring the poll phase
CDuring the timers phase
DDuring the close callbacks phase
✗ Incorrect
setImmediate() callbacks run in the check phase, which happens after the poll phase.
If the event loop is busy, what happens to timer callbacks?
AThey are delayed until the event loop is free
BThey are skipped
CThey run immediately
DThey run during the poll phase
✗ Incorrect
Timer callbacks wait until the event loop reaches the timers phase and is free to run them.
Which phase runs callbacks for socket or handle close events?
Atimers
Bclose callbacks
Ccheck
Dpoll
✗ Incorrect
The close callbacks phase runs callbacks for closed sockets or handles.
Explain the sequence of event loop phases and how timer callbacks are executed in Node.js.
Think about how the event loop cycles through phases and when timers get their turn.
You got /5 concepts.
Describe why a setTimeout callback might not run exactly after its delay time in Node.js.
Consider what else the event loop might be doing when the timer expires.
You got /4 concepts.
Practice
(1/5)
1. Which phase of the Node.js event loop executes setTimeout callbacks?
easy
A. Check phase
B. Timers phase
C. Poll phase
D. Close callbacks phase
Solution
Step 1: Understand event loop phases
The Node.js event loop has multiple phases, each handling different types of callbacks.
Step 2: Identify where timers run
The timers phase is specifically designed to execute callbacks scheduled by setTimeout and setInterval.
Final Answer:
Timers phase -> Option B
Quick Check:
Timers phase = setTimeout callbacks [OK]
Hint: Timers run in the timers phase, not immediately [OK]
Common Mistakes:
Confusing timers phase with poll phase
Thinking setTimeout runs immediately
Mixing check phase with timers phase
2. Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?
easy
A. setTimeout(console.log('Hi'))
B. setTimeout(console.log('Hi'), 0);
C. setTimeout(0, () => console.log('Hi'));
D. setTimeout(() => console.log('Hi'), 0);
Solution
Step 1: Check function syntax for setTimeout
The first argument must be a function, not the result of a function call.
Step 2: Analyze each option
setTimeout(() => console.log('Hi'), 0); passes a function that logs 'Hi' after 0 ms delay correctly. setTimeout(console.log('Hi'), 0); calls console.log immediately and passes its result (undefined). setTimeout(0, () => console.log('Hi')); passes 0 (number) as first argument instead of a function, causing a TypeError on execution. setTimeout(console.log('Hi')) calls console.log immediately without delay argument.
Final Answer:
setTimeout(() => console.log('Hi'), 0); -> Option D
Quick Check:
Function as first argument = setTimeout(() => console.log('Hi'), 0); [OK]
Hint: Pass a function, not a function call, to setTimeout [OK]
Common Mistakes:
Calling the function immediately inside setTimeout
Omitting the delay argument
Passing non-function as first argument
3. What will be the output order of the following code?
Step 1: Identify synchronous, microtask, and timers
console.log('A') and console.log('D') run immediately. Promise.then callbacks run in microtasks after current code. setTimeout callbacks run in timers phase later.
Step 2: Determine execution order
Output order is synchronous logs first (A, D), then microtasks (C), then timers (B).
Final Answer:
A, D, C, B -> Option A
Quick Check:
Microtasks run before timers [OK]
Hint: Promise.then runs before setTimeout even with 0 delay [OK]