This article mainly introduces the use of Process.nexttick in Node.js, Nexttick function What the use, how to use, and settimeout what is the difference, this article explains the knowledge, the need for friends can refer to the
I don't remember where I first saw process.nexttick this thing, oh, it should be in the official Nodejs process document. At that time did not understand what this thing is doing, have already had settimeout, still need this function to do. And fundamentally, what does this function do? What's the difference from settimeout?
There is a very good post on StackOverflow that basically explains my problem, and here I enclose the link and give an example of it:
Stackoverflow.com >> What are the proper use cases for Process.nexttick in Node.js?
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25 |
var myconstructor = function () {... process.nexttick (function () {self._continue ();}); myconstructor.prototype.__proto__ = Eventemitter.prototype; Myconstructor.prototype._continue = function () {//without the Process.nexttick//These events would be emitted immediat Ely//With no listeners. They would be lost. This.emit (' Data ', ' hello '); This.emit (' Data ', ' world '); This.emit (' End '); }; Function (req, res, next) {var c = new Myconstructor (...); C.on (' Data ', function (data) {Console.log (data);}); C.on (' End ' , next); } |
Simply because the relationship of the asynchronous model causes some code to execute before the required conditions are complete, place the code that requires the preconditions into a callback function and then put it on top of the next event loop. The code is not executed immediately, but waits before the next round of events starts and executes after it starts.