问题描述
今天发布了Node.js版本0.10并引入了 setImmediate
。 文档建议在进行递归 nextTick
调用时使用它。
Node.js version 0.10 was released today and introduced setImmediate
. The API changes documentation suggests using it when doing recursive nextTick
calls.
从它看起来非常类似于 process.nextTick
。
From what MDN says it seems very similar to process.nextTick
.
我什么时候应该使用 nextTick
我什么时候应该使用 setImmediate
?
When should I use nextTick
and when should I use setImmediate
?
推荐答案
如果要排队,请使用 setImmediate
在任何已经在事件队列中的I / O事件回调后面的函数。使用 process.nextTick
有效地将函数排在事件队列的头部,以便在当前函数完成后立即执行。
Use setImmediate
if you want to queue the function behind whatever I/O event callbacks that are already in the event queue. Use process.nextTick
to effectively queue the function at the head of the event queue so that it executes immediately after the current function completes.
因此,在您尝试使用递归分解长时间运行的CPU绑定作业的情况下,您现在想要使用 setImmediate
而不是 process.nextTick
将下一次迭代排队,否则任何I / O事件回调都无法在迭代之间运行。
So in a case where you're trying to break up a long running, CPU-bound job using recursion, you would now want to use setImmediate
rather than process.nextTick
to queue the next iteration as otherwise any I/O event callbacks wouldn't get the chance to run between iterations.
这篇关于setImmediate与nextTick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!