process is the core module of Node JS and we don?t have a need to install this module. You can simply access this module by require method.
const process = require(‘process’)
If you don?t want to require process module, you directly use it.
By the use of an above snippet of code, you access all the environment variables and values of the system in Node JS app. Like process.env, you will access all the methods of process from any of the files in Node JS app without requiring it. process.nextTick ( ) is one of the methods of process module.
What is process.nextTick ( ) ?
When in Node JS, one iteration of the event loop is completed. This is known as a tick. process.nextTick() taking a callback function which is executed after completing the current iteration/tick of the event loop.
Phases of Event Loop in Node JS from nodejs.org
In the above diagram, process.nextTick ( ) is not the part of any phase of event loop. Instead, nextTickQueue will process all the callbacks after completing current iteration and before starting the next iteration of event loop.
process.nextTick ( ) schedule a callback function to be executed in next iteration of event loop.
Output:Executed in current iterationExecuted in next iteration
The above code of snippet, the second console is printed first because this is a part of the current iteration of the event loop and the first console is a part of a callback function which is associated with process.nextTick() executed in next iteration of event loop.
When we use process.nextTick ( ) ?
- To cleanup unwanted resources.
- To allow users to handle errors.
- To try a request to run before starting the next iteration of event loop.
- Allow a callback to run after call stack and before next iteration of event loop.