Use the process. nextTick instance in Node. js

Source: Internet
Author: User
Tags emit

Use the process. nextTick instance in Node. js

This article mainly introduces Node. process. if nextTick is used as an example, what is the use and use of nextTick functions? What is the difference between nextTick and setTimeout? This article explains this knowledge. For more information, see.

I don't remember where I first saw the call of process. nextTick. Oh, it should have been 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?

?

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 wocould be emitted immediately

// With no listeners. they wocould 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.

Related Article

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.