[英]Promise.resolve().then vs setImmediate vs nextTick
NodeJS 0.11 as well as io.js and the Node 0.12 branch all ship with native promises. NodeJS 0.11以及io.js和Node 0.12分支都附带本机承诺。
Native promises have a .then
method which always executes on a future event loop cycle. 本机promise 有一个
.then
方法 ,它总是在未来的事件循环周期中执行。
So far I've been using setImmediate
to queue things to the next iteration of the event loop ever since I switched from nextTick : 到目前为止,自从我从nextTick切换以来,我一直在使用
setImmediate
将事物排队到事件循环的下一次迭代:
setImmediate(deferThisToNextTick); // My NodeJS 0.10 code
process.nextTick(deferThisToNextTick); // My NodeJS 0.8 code
Since we now have a new way to do this: 因为我们现在有了一种新方法:
Promise.resolve().then(deferThisToNextTick);
Which should I use? 我应该使用哪个? Also - does
Promise.resolve.then
act like setImmediate
or like nextTick
with regards to code running before or after the event loop? 另外 - 对于在事件循环之前或之后运行的代码,
Promise.resolve.then
就像setImmediate
或者像nextTick
一样吗?
Using Promise.resolve().then
has no advantages over nextTick
. 使用
Promise.resolve().then
没有优于nextTick
优势。 It runs on the same queue, but have slightly higher priority, that is, promise handler can prevent next tick callback from ever running, the opposite is not possible. 它运行在同一个队列中,但具有稍高的优先级,也就是说,promise处理程序可以防止下一个tick回调从而运行,相反是不可能的。 This behaviour is an implementation detail and should not be relied on.
此行为是一个实现细节,不应该依赖。
Promise.resolve().then
is obviously slower (a lot, I think), because it creates two promises which will be thrown away. Promise.resolve().then
显然更慢(很多,我认为),因为它创建了两个将被丢弃的承诺。
You can find extensive implementation info here: https://github.com/joyent/node/pull/8325 您可以在此处找到大量的实施信息: https : //github.com/joyent/node/pull/8325
The most important part: Promise.resolve().then
is like nextTick
and not like setImmediate
. 最重要的部分:
Promise.resolve().then
就像nextTick
而不是setImmediate
。 Using it n place of setImmediate
can change your code behaviour drastically. 使用它而不是
setImmediate
可以彻底改变你的代码行为。
I'm not going to answer the bolded part about technicalities, but only the question 我不会回答关于技术性问题的粗体部分,而只回答问题
Which should I use?
我应该使用哪个?
I don't think there is any reason to use Promise.resolve().then()
unless you are interested in the promise for the result of your asynchronously executed function. 我认为没有任何理由使用
Promise.resolve().then()
除非您对异步执行函数的结果的承诺感兴趣。 Of course, if you are , then this would be far superior than dealing with callback hell or making a new Promise
from setTimeout
or nextTick
. 当然,如果你是 ,那么这将远远优于处理回调地狱或从
setTimeout
或nextTick
创建一个new Promise
。
There's also a second technical difference, more import than the timing: promises do swallow exceptions. 还有第二个技术差异,比时间更重要:承诺可以吞下例外。 Which you probably don't want.
你可能不想要的。 So, like @vkurchatkin mentioned, don't create promises only to throw them away.
因此,就像@vkurchatkin所提到的那样,不要创造承诺只是为了抛弃它们。 Not only because it's slower, but because it makes your code less readable and your app more error-prone.
不仅因为它速度较慢,而且因为它使您的代码可读性降低,而且您的应用程序更容易出错。
Promise.resolve将立即(同步)解决,而setImmediate在执行当前事件后明确地直接解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.