12/17/2022 0 Comments Nodejs event loop![]() The point to be noted that every time the event loop takes a complete trip, we would call it a tick. The process.nextTick(callback ) method have taken a callback and an optional args parameters and queued in the "next tick queue". ![]() This method allows the developers to accept a function's execution until the subsequent Iteration of the event loop. Moreover, process.nextTick() is one of the methods of process module. Similarly, the process.env function provides access to all the process module methods in Node.js without importing with the required () function. Using the above snippet of code, we can easily access all the environment variables and values of Node.js' system. However, there is another way to use the process module directly without using the require() function: process.env We can import the process module by the require() function: const process = require(‘process’) The process module is one of the global modules provided by the core API of Node.js. ![]() However, they are processed at separate phases of the event loop. Both of these functions allow the users to schedule the callback handlers in the loop of events. These functions includes process.nextTick() and set.Immediate() respectively. These functions allow the users to control or handle the sequence of the code's execution in the event loop. Then it enters the Poll phase, and since there are no more timers scheduled by setImmediate, it waits for the callback of the file reading operation and executes the callback: console.log('reading file') Īnd the event loop repeats iterations until it has nothing to do, so the Node.js process ends.Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase of the event loop. So the event loop continues the second iteration and enters the Timer phase again to execute the remaining callback in its callback queue: console.log('setTimeout 2') console.log('setInterval') ![]() So far, the first iteration of the event loop has been completed. Therefore, the Poll phase will stay idle for a while, and the event loop will proceed to the Check phase because there's a timer scheduled by setImmediate().ĭuring the Check phase, the callback scheduled by setImmediate()will be executed : console.log('setImmediate') But it takes some time to complete the file reading operation on /etc/passwd. Next, the callbacks in the Poll phase should be executed. However, each callback execution is in a separate iteration. In the Timer phase, there’s a callback scheduled by setInterval() and fired repeatedly in an interval of 0ms (4 times until the timer is canceled). The second callback may not be executed during the first iteration because it expires in 3ms, so its execution would be deferred to the next iteration. Once the main thread clears the microtask queue, the execution enters the Timer phase of the event loop and executes the callbacks with an expired timer: console.log('setTimeout 1') console.log('setInterval') Then the callbacks in the microtask queue registered by process.nextTick(), then() and catch() in the main thread: console.log('nextTick') console.log(data) // Promise 1 The JavaScript code in the main thread is executed synchronously first: console.log('start') console.log('start promise 1') console.log('end') ![]() Let’s look at a code snippet first: setImmediate(()=> console.log('setImmediate')) fs.readFile('/etc/passwd',(err, data)=>) console.log('end') The different phases and the tasks that each phase handle ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |