0
0
Node.jsframework~5 mins

Event loop mental model in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
Which of these is NOT a phase in the Node.js event loop?
ARender
BTimers
CPoll
DCheck
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
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
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.