Process.nexttick usages in Node.js _node.js

Source: Internet
Author: User
Tags emit

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: >> 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.

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: 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.