Event Loop

It is one of the most important components of the Node.js runtime. It handles all the asynchronous events in the application and calls the corresponding callback function in the node program when the event is completed.

It works something like this:

while (!shouldExit) {
    processEvent();
}

It basically runs until Node is ready to exit and processes all the events that are sent here.

Each phase of the Event Loop is responsible for different types of operations. There are 44 such phases:

  1. Timers
  2. I/O callbacks
  3. setImmediate
  4. Close callbacks

Each of these phases have their separate Callback Queue.

Event Loop Phases execution strategy It starts by looking at the timer queue and checks if there are any functions that are available to execute i.e., functions for timers that have completed. It then moves on to I/O callback. And after that, it checks for any setImmediate functions. In the end, it checks for Close Callbacks i.e., callbacks when a file or DB connection is closed. It then starts again from the top (hence the name 'Event Loop').


References:


Children
  1. Callback Queue
  2. Event Loop Phases

Backlinks