The Node.js Event Loop Part 5

Опубликовано: 20 Май 2026
на канале: MBCode
24
1

The Node.js event loop. The event loop is a set of six operations or phases that perform some very specific tasks. It's implemented by this C library called libuv. You can read more about, it can create GitHub site, it's a quite extensive library. But this library is also responsible for keeping Node.js running with its non-blocking or asynchronous operations. What happens is when it receives a request, this request if it contains any long operations, things like I/O operations, for example, reading and writing files, or DB connections, or DB queries, and things like that, they will offload these operations to the system kernel. While it's doing that at the background, the event loop keeps processing on this request and so forth. To understand or see this little bit better, let's go back and look at the processing model. Here is the Node.js processing model we've seen earlier. A request comes in, the event loop process that request, if there's any running operations it send that off to the back for a tripple to processes those information, when they're done, they send back to the event loop and then it will send that up to the user. Let's take a look inside this red arrow area where all those phases occur. Here is the event loop cycle. We have the Node.js, single thread, a non-block, and event loop here. The first phase is the timer. This phase is responsible for executing any callbacks that are scheduled by two very important functions, the set timeout and set interval. The second phase is depending callbacks. This one here executes any I/O callbacks and defer it to the next loop iteration. Phase 3, the idle and prepare, it's an internal use only phase. It's responsible for executing any callbacks for some system operations like TCP errors and so forth. The fourth phase is a really important one. This phase is responsible for two main functions. One, is it calculates how long it should block or poll for any I/O events or operations. Then two, it'll process events in the poll queue, and only that is also responsible for controlling when timers should execute. The next phase, phase 5, is where it allows you, the developer, to execute any callbacks immediately after the polled phase 4. Mostly here, the most important function here is the set immediate function, and the callbacks in that function are invoked immediately and let them right in this phase. The last phase, phase 6, is the close callback. This is more of a cleanup phase where it will close some callbacks, especially we do a socket I/O or connection, then when you close a connection and then it'll handle all these events, the close events rather at the particular phase. This is the event loop cycle, has this six major phases, and we'll be referring to this cycle again in a future video. Make sure you have a clear understanding of what these phases are. In the next video, we're going to take a closer look at one of the most common cycles in the Node.js event loop, the I/O cycle.