Promise / A + spec第2.2.4条的目的是什么? [英] What is the intention behind clause 2.2.4 of Promise/A+ spec?

查看:131
本文介绍了Promise / A + spec第2.2.4条的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

承诺/ a +规范第2.2.4条规定:

Clause 2.2.4 of the promise/a+ spec says:


onFulfilled或onRejected在执行$ b $之前不得被调用b上下文堆栈仅包含平台代码。

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

然后在备注中说明:


这里平台代码表示引擎,环境和promise
实现代码。实际上,这个要求确保
onFulfilled和onRejected异步执行,之后调用
循环,然后调用一个新堆栈。

Here "platform code" means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack.

这是为了确保当链中存在大量onFulfilled函数时,它们的执行不会导致线程阻塞吗?

Is the intention of this to ensure that when there is a large amount of onFulfilled functions in a chain, the execution of them does not cause the thread to block?

或者我没有阅读的行之间有什么其他内容吗?

Or is there anything else that is between the lines that I am not reading?

推荐答案

原因是,当回调始终异步而不是可能异步时,它会提供更加一致和可靠的api。请考虑以下代码

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;

现在该片段明确假设 onFulfilled 将不要马上打电话,如果不是这样,我们很快就会把未使用的披萨放在周围,我们就会感到饥饿。虽然在这种情况下,错误很容易修复,但执行的顺序更容易遵循,因此当您可以做出这样的假设时,api更容易使用。

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.

关于Promise / A + GitHub回购的问题已经关闭讨论这个。

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

这篇关于Promise / A + spec第2.2.4条的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆