简体   繁体   English

是否保证此Promise链按此顺序执行?

[英]Is this Promise chain guaranteed to execute in this order?

 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } let p = sleep(50); p.then(() => console.log('a')).then(() => console.log('c')); p.then(() => console.log('b')).then(() => console.log('d')); 

Is this guaranteed to print "a, b, c, d" in that order? 是否保证以此顺序打印“ a,b,c,d”?

As far as I can tell, "a" has to fire before "c" and "b" has to fire before "d", but beyond that, can the JS interpreter decide to execute the remainder in a different order? 据我所知,“ a”必须在“ c”之前执行,而“ b”必须在“ d”之前执行,但是除此之外,JS解释器可以决定以其他顺序执行其余操作吗?

The way that things are queued using setTimeout is exactly that - a queue. 使用setTimeout将事物排队的方式就是队列。 If two callbacks are queued with the same 'delay', the callback that was queued first, will fire first. 如果两个回调使用相同的“延迟”排队,则首先排队的回调将首先触发。

Edit: I failed to understand the OP's intention initially. 编辑:我最初不了解OP的意图。

'Branching' promises is what is actually occurring here. “分支”承诺是这里实际发生的事情。 Meaning - the 'then-able' being referenced in the first set of then-ables (for a & b) will fire the two provided callbacks at 'the same time' because they both reference the same promise - however - the tricky bit is that they execute in the order that they were queued using the .then(...) of the resolving promise object. 含义-在第一组then-ables中(为a和b)引用的“ then-able”将在“同时”触发两个提供的回调,因为它们都引用了相同的promise-但是,棘手的是它们按照使用解决承诺对象的.then(...)排队的顺序执行。

Then the following/subsequent callbacks are queued in their respective orders (c & d). 然后,以下/后续回调按其各自的顺序(c&d)排队。

To answer the question directly: No. The nature of the async actions in a then-able could be anything. 要直接回答这个问题:否。那么一个对象中的异步动作的性质可以是任何东西。 However, the functions provided in the OP's then-ables are essentially synchronous, resulting in the intuitive - but entirely misleading - logging order. 但是,OP的then-ables中提供的功能基本上是同步的,从而导致了直观的日志记录顺序,但完全是误导性的。

As far as I can tell, "a" has to fire before "c" and "b" has to fire before "d" 据我所知,“ a”必须在“ c”之前触发,“ b”必须在“ d”之前触发

Yes, that much for certan. 是的,certan就这么多。

beyond that, can the JS interpreter decide to execute the remainder in a different order? 除此之外,JS解释器是否可以决定以其他顺序执行其余部分?

Depends on whom you ask, but no there are more guarantees that make the output more predictable: 取决于您询问的人,但是没有其他保证可以使输出更可预测:

  • the Promise/A+ specification also demands that " all respective callbacks must execute in the order of their originating calls to then. " So in your example, that means "a" has to fire before "b", because the callback was chained first to p . Promise / A +规范还要求“ 所有各自的回调都必须按照对它们的原始调用的顺序执行。 ”因此,在您的示例中,这意味着“ a”必须在“ b”之前触发,因为该回调首先链接到p
  • the ECMAScript specification defines a job queue for promise callbacks, nailing down the order (imo unnecessarily). ECMAScript规范为承诺回调定义了一个工作队列,从而确定了顺序(不必要地是imo)。 It conforms to Promises/A+ in that the "a" and "b" callbacks are queued in the order they were set up when the promise fulfills. 它与Promises / A +一致,因为“ a”和“ b”回调以在实现诺言时设置的顺序排队。 It also means that after "a" returns and fulfills the promise, it will schedule "c", and after "b" returns and fulfills the promise, it will schedule "d", so their order is determined as well (for synchronous callbacks). 这也意味着在“ a”返回并履行承诺之后,它将安排“ c”,而在“ b”返回并履行承诺之后,它将安排“ d”,因此也确定了它们的顺序(对于同步回调)。

In general, don't rely on any scheduling algorithm, if you require a certain order then make it explicit. 通常,不要依赖任何调度算法,如果您需要确定的顺序,则将其明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM