This article mainly introduces Node. process. nextTick uses an instance. what is the use, use, and difference between the nextTick function and setTimeout? this article explains this knowledge, for more information, see where I can see process for the first time. nextTick is called. Oh, it should be seen in the official process document of nodejs. At that time, I didn't understand what this was doing. all of them already had setTimeout. What else should I do with this function. And basically, what does this function do? What is the difference with setTimeout?
Stackoverflow has a very good post that basically explains my problem. here I will attach a link and give an example in 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 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 put, because of the relationship between asynchronous models, some code may be executed before the conditions required by them are completed. Therefore, these code that requires conditions must be put into a callback function, then, it is placed on the top of the next event loop. The code will not be executed immediately, but will wait before the next round of event is started and then be executed.