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 with 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?
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 Immediately
//with no listeners. They would to 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 of the relationship of the asynchronous model, which 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.