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 such phases:
- Timers
- I/O callbacks
- setImmediate
- Close callbacks
Each of these phases have their separate Callback Queue.
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
Backlinks