This article will give you a detailed introduction to global functions under Node.js. I hope it will be helpful to friends in need!
1. Global functions under Node.js
1.1 The global functions provided by JavaScript language are still available under Node.js
• parseInt/parseFloat/isNaN/isFinite/eval..
• One-time timer (setTimeout / clearTimeout)
• Periodic timing (setInterval / clearInterval)
1.2 The Node.js environment also provides some global functions
• Immediate execution of timer (setImmediate / clearImmediate)
Run the main program first. After the main program finishes running, the nextTick function is the end of the main program. It is executed immediately after the main process ends. Then run the setImmediate function, the event queue head function, which is executed immediately before the event queue starts. Finally, the setTimeout function in the event queue is executed.
The main program is synchronous, and the event queue is asynchronous. When the main program is executed, the event queue will be notified for execution. If the function is not loaded immediately, such as setTimeout, it will be lost in the event queue. When the main program is executed, it will be notified. The event queue is then executed sequentially from the head to the inside.
1.3 The Node.js environment also provides other global functions
• Immediate execution of the timer (setImmediate / clearImmediate) just mentioned.
• The process immediately executes the timer (process.nextTick) , the second function provided by the Node environment.
1.4 Example
Through the example, we see that the execution order is
1.num variable printing.
2. The process.nextTick() function is executed immediately after the main program ends after the variable is printed.
3. The setImmediate() function, after the tail function of the main program is executed, executes the function at the head of the event queue.
4. The setTimeout() function is an internal function executed after the function at the head of the event queue is executed.
[Recommended: node.js video tutorial]
The above are the global functions provided by the Node.js environment! For more details, please pay attention to other related articles on the php Chinese website!