Phases of Event loop of Node js
The Node.js event loop has several phases that it goes through to manage asynchronous operations. The five main phases of the Node.js event loop are:
Poll: In this phase, Node.js will check for new I/O events and execute any callbacks that are waiting for them. If there are no I/O events, Node.js will wait for them to occur.
Check: This phase is for executing callbacks that were deferred by the setTimeout and setInterval functions.
Timer: In this phase, Node.js will execute callbacks that were scheduled by the setTimeout and setInterval functions.
Pending callback: This phase is for executing I/O callbacks that were deferred to the next loop iteration.
Close callback: This phase is for executing any close callbacks that were scheduled by the process.nextTick function.
Each phase of the event loop has a priority order, with the Poll phase having the highest priority, followed by Check, Timer, and Pending Callback. The Close Callback phase is the lowest priority phase. Once all callbacks in a phase are executed, the event loop moves to the next phase.
Poll Phase:
const fs = require('fs'); // read a file asynchronously fs.readFile('myfile.txt', (err, data) => { if (err) { throw err; } console.log(data); }); // schedule a function to execute after 1 second setTimeout(() => { console.log('Timeout callback executed'); }, 1000); // start the event loop while (true) { // do some work }
In this example, the
fs.readFile
function is executed asynchronously, and its callback function will be executed during the Poll phase of the event loop when the file data is available. ThesetTimeout
function is also scheduled to execute after 1 second, but it will not be executed until the Poll phase has completed.Check Phase:
// schedule a function to execute after 1 second setTimeout(() => { console.log('Timeout callback executed'); }, 1000); // schedule a function to execute during the Check phase setImmediate(() => { console.log('Immediate callback executed'); }); // start the event loop while (true) { // do some work }
In this example, the
setTimeout
function is scheduled to execute after 1 second, and its callback function will be executed during the Timer phase of the event loop. ThesetImmediate
function, on the other hand, is scheduled to execute during the Check phase, so its callback function will be executed immediately after the Poll phase has completed.Timer Phase:
// schedule a function to execute after 1 second setTimeout(() => { console.log('Timeout callback executed'); }, 1000); // start the event loop while (true) { // do some work }
In this example, the
setTimeout
function is scheduled to execute after 1 second, and its callback function will be executed during the Timer phase of the event loop.Pending Callback Phase:
const http = require('http'); // create an HTTP server const server = http.createServer((req, res) => { res.end('Hello World!'); }); // listen on port 3000 server.listen(3000, () => { console.log('Server started'); }); // start the event loop while (true) { // do some work }
In this example, the
http.createServer
function creates an HTTP server and registers a callback function to handle incoming requests. The server will not start listening for requests until the event loop has completed its current iteration and entered the Pending Callback phase.Close Callback Phase:
const fs = require('fs'); // open a file const file = fs.createReadStream('myfile.txt'); // register a callback function to be executed when the file is closed file.on('close', () => { console.log('File closed'); }); // start the event loop while (true) { // do some work }
In this example, the
fs.createReadStream
function creates a readable stream for a file and registers a callback function to be executed when the file is closed. The callback function will be executed during the Close Callback phase of the event loop after the file has been read and the stream has been closed.
The above topic is for having great knowledge of node js and event loop by using their phases, most of the people don't even know about this, but now you will know this with the code example as well....
Hope I was able to clear my point here, if you have still doubt(s) or suggestion(s) then please comment me I will try to reply as soon as possible.