Event Loop
The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded
This is powered by libuv.
Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.
- Each phase has it’s own FIFO queue of callbacks to execute
- On entering each phase, it will perform any operations specific to that phase, then execute callbacks in that phase’s queue until the queue has been exhausted or the maximum number of callbacks has executed
Phases
- Timers: execute any
setTimeout()
andsetInterval()
callbacks given that enough time has passed since they were scheduled - Pending callbacks: certain types of I/O callbacks (i.e. TCP
ECONNREFUSED
) - Idle, prepare: Node internals
- Poll: wait for system to call us back for I/O events (normally, this is where Node chooses to block)
- Check:
setImmediate()
- Close callbacks:
socket.on('close', ...)