What is the intention behind clause 2.2.4 of Promise/A+ spec?

后端 未结 1 1494
情深已故
情深已故 2020-11-28 16:37

Clause 2.2.4 of the promise/a+ spec says:

onFulfilled or onRejected must not be called until the execution context stack contains only platform code

相关标签:
1条回答
  • 2020-11-28 17:04

    The reasoning is that when the callbacks are always asynchronous instead of possibly asynchronous, it gives more consistent and reliable api to use. Consider the following code

    var pizza;
    browseStackOverflow().then(function(){
        eatPizza(pizza);
    });
    pizza = yesterdaysLeftovers;
    

    Now that snippet clearly assumes the onFulfilled will not be called right away and if that wasn't the case we would soon have unused pizza lying around and we'd be left hungry. Although in this case the bug would be easy enough to fix, the order of execution is easier to follow and thus the api is easier to use when you can make some assumptions like this.

    There is a closed issue on the Promises/A+ GitHub repo with discussion about this.

    0 讨论(0)
提交回复
热议问题