繁体   English   中英

分支 Promise 的执行顺序

[英]Order of execution of branching Promises

是否有关于 JavaScript Promise 分支执行顺序的规范?

考虑这个代码:


// Create a promise that will be resolved when we call the function `r`
let r;
const promise = new Promise((resolve, reject) => {
    r = resolve; // Store the resolver in `r`, so we can call it later.
});

// When `promise` resolves, print "I am the first branch".
promise.then(() => console.log("I am the first branch"));

// When `promise` resolves, also print "I am the second branch".
promise.then(() => console.log("I am the second branch"));


// Resolve `promise`
r();

Promise promise分为两个分支。

根据我的测试,第一个分支在第二个分支之前执行。

I am the first branch
I am the second branch

我的问题:这是由 JavaScript 规范保证的,还是我依赖​​于未定义的行为?

注意:当然,我可以将 promise 链接起来。 像这样:

promise.then(() => console.log("I am the first branch")).then(() => console.log("I am the second branch"));

但如果可能的话,我更愿意将它们分支。

在这种情况下(-> 在同一个承诺上多次调用.then() )...

 const p = new Promise((resolve, reject) => { setTimeout(resolve, 1000) }); p.then(() => console.log("first")); p.then(() => console.log("second"));

...顺序是有保证的,因为.then()就像一个带有数组的.push() 规范中的相关路径是:

Promise.prototype.then 中的第 5 Promise.prototype.then + PerformPromiseThen 中的第 9.a PerformPromiseThen

27.2.5.4 Promise.prototype.then ( onFulfilled, onRejected )

当使用参数onFulfilledonRejected调用 then 方法时,将执行以下步骤:

 1. Let promise be the this value. 2. If IsPromise(promise) is false, throw a TypeError exception. 3. Let C be ? SpeciesConstructor(promise, %Promise%). 4. Let resultCapability be ? NewPromiseCapability(C). 5. Return PerformPromiseThen(promise, onFulfilled, onRejected, resultCapability).

然后

27.2.5.4.1 PerformPromiseThen ( promise, onFulfilled, onRejected [ , resultCapability ] )

抽象操作PerformPromiseThen接受参数 promise、 onFulfilledonRejected和可选参数resultCapability (一个PromiseCapability记录)。 它使用onFulfilledonRejected作为结算动作对 promise 执行“ then ”操作。 如果通过resultCapability则通过更新resultCapability的承诺来存储结果。 如果未通过,则PerformPromiseThen将由规范内部操作调用,其中结果无关紧要。 它在调用时执行以下步骤:

 1. Assert: IsPromise(promise) is true. 2. If resultCapability is not present, then a. Set resultCapability to undefined. 3. If IsCallable(onFulfilled) is false, then a. Let onFulfilledJobCallback be empty. 4. Else, a. Let onFulfilledJobCallback be HostMakeJobCallback(onFulfilled). 5. If IsCallable(onRejected) is false, then a. Let onRejectedJobCallback be empty. 6. Else, a. Let onRejectedJobCallback be HostMakeJobCallback(onRejected). 7. Let fulfillReaction be the PromiseReaction { [[Capability]]: resultCapability, [[Type]]: Fulfill, [[Handler]]: onFulfilledJobCallback }. 8. Let rejectReaction be the PromiseReaction { [[Capability]]: resultCapability, [[Type]]: Reject, [[Handler]]: onRejectedJobCallback }. 9. If promise.[[PromiseState]] is pending, then a. Append fulfillReaction as the last element of the List that is promise.[[PromiseFulfillReactions]]. b. Append rejectReaction as the last element of the List that is promise.[[PromiseRejectReactions]]. 10. Else if promise.[[PromiseState]] is fulfilled, then a. Let value be promise.[[PromiseResult]]. b. Let fulfillJob be NewPromiseReactionJob(fulfillReaction, value). c. Perform HostEnqueuePromiseJob(fulfillJob.[[Job]], fulfillJob.[[Realm]]). 11. Else, a. Assert: The value of promise.[[PromiseState]] is rejected. b. Let reason be promise.[[PromiseResult]]. c. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "handle"). d. Let rejectJob be NewPromiseReactionJob(rejectReaction, reason). e. Perform HostEnqueuePromiseJob(rejectJob.[[Job]], rejectJob.[[Realm]]). 12. Set promise.[[PromiseIsHandled]] to true. 13. If resultCapability is undefined, then a. Return undefined. 14. Else, a. Return resultCapability.[[Promise]].

暂无
暂无

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

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