It is tricky to understand how Node.js is single-threaded yet can perform non-blocking
Event loop, as the name suggests, is a single-thread, loop that is almost infinite. We say almost infinite as it has some hard limits if it does find itself in an infinite loop. The reason this single-threaded loop can perform otherwise blocking operations in a non-blocking manner is due to offloading. Node.js offloads operations to the system kernel whenever possible. The loop in event loop refers to Node.js cycling through operations in order. We call these operations phases. Let’s take a look at the event loop.
Let’s breakdown each phase to see what is happening.
This phase executes callbacks scheduled by setTimeout(
) and setInterval()
. It should be noted that a timer denotes the minimum time Node.js will wait before executing the callback. The actual delay might be slightly longer depending on the poll phase.
This phase executes I/O callbacks that were deferred to the next loop iteration. Some system operations want to wait before executing a callback, this phase is used for that.
This phase is used internally by Node.js. There isn’t a lot of documentation on this phase, but it is mentioned in the official documentation.
This phase performs two operations. First, it calculates how long it should block and poll for I/O. Then, it processes the events in the poll queue.
This phase is used to invoke setImmediate()
callbacks. If the poll phase is empty or callbacks using setImmediate()
have been queued, the event loop might skip the poll phase and continue to the check phase.
Methods such as socket.destroy
Free Resources