Node.js中process.nextTick的正确用例是什么? [英] What are the proper use cases for process.nextTick in Node.js?

查看:113
本文介绍了Node.js中process.nextTick的正确用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过 process.nextTick 在一些地方使用,并不能完全告诉它用于什么。

I have seen process.nextTick used in a few places and can't quite tell what it's being used for.

  • https://github.com/andrewvc/node-paperboy/blob/master/lib/paperboy.js#L24
  • https://github.com/substack/node-browserify/blob/master/index.js#L95

Node.js中 process.nextTick 的主要/正确用例是什么?文档基本上说这是一种更优化的方式来做 setTimeout ,但这没什么用。

What are the main/proper use cases of process.nextTick in Node.js? The docs basically say it's a more optimized way of doing setTimeout, but that doesn't help much.

I曾经做过很多ActionScript,所以等到下一帧执行代码的想法在某种程度上是有意义的 - 如果你正在运行动画,你可以让它更新每一帧而不是每毫秒更新一次。当您想要协调设置一组变量时,它也是有意义的 - 您可以更改第1帧中的变量,并在第2帧中应用更改.Flex在其组件生命周期中实现了类似的功能。

I used to do a lot of ActionScript, so the idea of "waiting until the next frame" to execute code makes sense on some level - if you're running an animation you can have it update every frame rather than every millisecond for example. It also makes sense when you want to coordinate setting a bunch of variables - you change the variables in frame 1, and apply the changes in frame 2. Flex implemented something like this in their component lifecycle.

我的问题是,我应该在服务器端JavaScript中使用它?我没有看到任何地方,你需要这种微调的性能/流量控制。只是朝着正确的方向寻找一个点。

My question is, what should I be using this for in server-side JavaScript? I don't see any places right off the bat where you'd need this kind of fine-tuned performance/flow control. Just looking for a point in the right direction.

推荐答案

process.nextTick 将回调放入队列。此队列中的每个回调都将在事件循环的下一个滴答开始时执行。它基本上用作清除调用堆栈的方法。当文档说它像 setTimeout 时,意味着它就像使用 setTimeout(function(){...},1)在浏览器中。它具有相同的用例。

process.nextTick puts a callback into a queue. Every callback in this queue will get executed at the very beginning of the next tick of the event loop. It's basically used as a way to clear your call stack. When the documentation says it's like setTimeout, it means to say it's like using setTimeout(function() { ... }, 1) in the browser. It has the same use cases.

一个示例用例是,为某些需要绑定事件的对象创建构造函数。但是,您无法立即开始发送事件,因为实例化它的代码还没有时间绑定到事件。您的构造函数调用在调用堆栈中位于它们之上,如果您继续执行同步操作,它将保持这种状态。在这种情况下,您可以使用 process.nextTick ,然后再继续执行您要执行的操作。它保证使用构造函数的人有足够的时间来绑定事件。

One example use case would be, you create a constructor for some object that needs events bound to it. However, you can't start emitting events right away, because the code instantiating it hasn't had time to bind to events yet. Your constructor call is above them in the call stack, and if you continue to do synchronous things, it will stay that way. In this case, you could use a process.nextTick before proceeding to whatever you were about to do. It guarantees that the person using your constructor will have time enough to bind events.

示例:

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');
};

使用此构造函数的中间件示例

Example Middleware using this constructor

function(req, res, next) {
  var c = new MyConstructor(...);
  c.on('data', function(data) {
    console.log(data);
  });
  c.on('end', next);
}

这篇关于Node.js中process.nextTick的正确用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆