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 waits for and dispatches events or messages in Node.js. It allows Node.js to perform non-blocking I/O operations by offloading operations to the system kernel whenever possible.
Click to reveal answer
intermediate
What are the main phases of the Node.js event loop?
The main phases are: timers (setTimeout, setInterval callbacks), pending callbacks, idle/prepare, poll (I/O events), check (setImmediate callbacks), and close callbacks (e.g., socket close). Each phase has a FIFO queue of callbacks to execute.
Click to reveal answer
beginner
How does Node.js handle asynchronous callbacks with the event loop?
When an async operation completes, its callback is queued in the appropriate event loop phase. The event loop runs continuously, executing callbacks from these queues one by one, allowing Node.js to handle many operations without blocking.
Click to reveal answer
intermediate
What is the difference between setTimeout and setImmediate in the event loop?
setTimeout schedules a callback after a minimum delay in the timers phase. setImmediate schedules a callback to run immediately after the poll phase in the check phase. setImmediate callbacks usually run before timers if both are scheduled from within an I/O cycle.
Click to reveal answer
beginner
Why does a long-running synchronous task block the event loop?
Because the event loop runs on a single thread, a long synchronous task prevents the loop from moving to the next phase or executing other callbacks, causing delays and blocking all other operations until it finishes.
Click to reveal answer
Which phase of the Node.js event loop handles setImmediate callbacks?
ACheck phase
BPoll phase
CTimers phase
DClose callbacks phase
✗ Incorrect
setImmediate callbacks are executed in the check phase, which runs immediately after the poll phase.
What happens if you run a long synchronous function in Node.js?
AIt runs in parallel without blocking
BIt blocks the event loop and delays other callbacks
CIt is automatically split into smaller tasks
DIt runs in a separate thread
✗ Incorrect
Node.js runs on a single thread for JavaScript, so a long synchronous function blocks the event loop and delays other callbacks.
Which of these is NOT a phase in the Node.js event loop?
ARender
BTimers
CPoll
DCheck
✗ Incorrect
Render is not a phase in the Node.js event loop; it is related to browser rendering.
When does the event loop execute callbacks from the timers phase?
ABefore any I/O callbacks
BOnly when the poll queue is empty
CAfter the check phase
DAfter the specified timer delay has passed
✗ Incorrect
Callbacks in the timers phase run after their specified delay has passed.
How does Node.js achieve non-blocking I/O?
ABy running all code asynchronously by default
BBy using multiple JavaScript threads
CBy offloading I/O operations to the system kernel and using the event loop to handle callbacks
DBy using Web Workers
✗ Incorrect
Node.js offloads I/O operations to the system kernel and uses the event loop to handle callbacks when operations complete, enabling non-blocking behavior.
Explain how the Node.js event loop manages asynchronous operations.
Think about how Node.js waits for tasks and runs their callbacks without stopping other code.
You got /4 concepts.
Describe the difference between setTimeout and setImmediate in the event loop.
Consider when each callback runs relative to the event loop phases.
You got /3 concepts.
Practice
(1/5)
1. Which part of the Node.js event loop runs Promise callbacks before timers?
easy
A. I/O callbacks phase
B. Timers phase
C. Microtasks queue
D. Check phase
Solution
Step 1: Understand event loop phases
The event loop has phases: timers, I/O callbacks, idle, poll, check, close callbacks, and microtasks run between phases.
Step 2: Identify when Promise callbacks run
Promise callbacks are microtasks and run immediately after the current operation, before timers and I/O callbacks.
Final Answer:
Microtasks queue -> Option C
Quick Check:
Promises run in microtasks before timers [OK]
Hint: Remember: promises run before timers in microtasks [OK]
Common Mistakes:
Thinking timers run before promises
Confusing I/O callbacks with microtasks
Assuming check phase runs before microtasks
2. Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?
easy
A. setTimeout(myFunc, 0);
B. setInterval(myFunc, 0);
C. process.nextTick(myFunc);
D. setImmediate(myFunc, 0);
Solution
Step 1: Identify function to run after delay
setTimeout schedules a function after a specified delay in milliseconds.
Step 2: Check syntax for zero delay
Using setTimeout(myFunc, 0) runs myFunc after the current call stack is empty, effectively scheduling it soon.
Final Answer:
setTimeout(myFunc, 0); -> Option A
Quick Check:
setTimeout with 0 delay schedules function correctly [OK]
Hint: Use setTimeout(func, 0) to schedule next tick [OK]
Common Mistakes:
Using setInterval for one-time delay
Passing extra argument to setImmediate
Confusing process.nextTick with setTimeout syntax
3. What will be the output order of the following code?
Step 1: Identify synchronous and asynchronous parts
console.log('start') and console.log('end') run immediately (synchronously). setTimeout callback runs later. Promise callback runs as microtask after current stack.
Which line causes the earliest callback to run, and why might the output order be unexpected?
medium
A. process.nextTick runs earliest because it runs before microtasks
B. setTimeout runs earliest because timers run first
C. Promise.then runs earliest because promises run before nextTick
D. All callbacks run simultaneously
Solution
Step 1: Understand callback priorities
process.nextTick callbacks run immediately after the current operation, before promise microtasks and timers.
Step 2: Explain output order
Even though promises are microtasks, process.nextTick callbacks have higher priority and run first, which can surprise learners expecting promises first.
Final Answer:
process.nextTick runs earliest because it runs before microtasks -> Option A
Quick Check:
nextTick > promises > timers [OK]
Hint: nextTick runs before promises and timers [OK]
Common Mistakes:
Assuming timers run before nextTick
Confusing promise and nextTick order
Thinking callbacks run simultaneously
5. You want to run a CPU-heavy task without blocking the event loop in Node.js. Which approach best uses the event loop model to keep your app responsive?
hard
A. Run the task synchronously in the main thread
B. Use setTimeout to split the task into smaller chunks
C. Use process.nextTick to run the entire task immediately
D. Run the task inside a Promise without splitting
Solution
Step 1: Understand event loop blocking
Running a heavy task synchronously blocks the event loop, making the app unresponsive.
Step 2: Choose non-blocking approach
Splitting the task into smaller chunks with setTimeout allows the event loop to process other events between chunks, keeping responsiveness.
Final Answer:
Use setTimeout to split the task into smaller chunks -> Option B
Quick Check:
Split heavy tasks with timers to avoid blocking [OK]
Hint: Split heavy tasks with setTimeout to avoid blocking [OK]