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?
✗ 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?
✗ 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?
✗ 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?
✗ Incorrect
Callbacks in the timers phase run after their specified delay has passed.
How does Node.js achieve non-blocking I/O?
✗ 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.