Process. nextTick in Node. js uses instance _ node. js

Source: Internet
Author: User
Tags emit
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.