Examples of process.nexttick usages in Node.js

Source: Internet
Author: User
Tags emit

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.

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.