关于渴望评估承诺回调的规格
是否有有关“渴望”回调的“渴望”的规格 /实现详细信息? 我的意思是,假设我有
var promise = new Promise((resolve) => {
resolve()
});
console.log(promise); // there is no public field '<state>', but you can see it in the console of the dev tools
我看到Promise
已经实现;我期望Promise
内部实现会在以下时间拨打Resolve
回调,并带有Promise> Promise
仍然无法满足的“时间窗口”。
这是对Resolve
回调的“急切”评估?这只是实现细节吗?
Is there a spec / implementation detail on how "eager" is the evaluation of the callbacks of a promise?
I mean, let's say that I have
var promise = new Promise((resolve) => {
resolve()
});
console.log(promise); // there is no public field '<state>', but you can see it in the console of the dev tools
I see that promise
is already fulfilled; I was expecting that the Promise
internal implementation would call the resolve
callback on an following time, leaving a "time window" with promise
still unfulfilled.
Is this "eager" evaluation of the resolve
callback "by design"? Is this just an implementation detail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回调传递给了承诺构造器,该构造函数同步执行。因此,当
new
操作员返回构造的对象时,回调已经运行。这是由 ecma脚本规范。
新的Promise(剥夺者)
的过程在步骤9中描述了执行程序被调用,并且在步骤11中,返回了构造的承诺对象。Mozilla贡献者
构造函数回调函数通常用于启动异步过程(通过依靠异步API,例如
settemeimout
),该过程可以在构造函数回调已经返回的时间时解决承诺。在您的示例中,该回调不依赖于异步API,而是同步调用
resolve
回调。这意味着,当将构造的承诺对象分配到您的Promise
变量时,该承诺已经处于满足状态。The callback passed to the Promise constructor is executed synchronously by that constructor. So when the
new
operator returns the constructed object, the callback has already run.This is by the ECMA Script specification. The procedure for
new Promise(excecutor)
describes in step 9 that the executor is called and in step 11 that the constructed promise object is returned.Mozilla Contributors write:
The constructor callback function is typically used to initiate an asynchronous process (by relying on an asynchronous API, like
setTimeout
), which can resolve the promise at a time that the constructor callback has already returned.In your example, that callback does not rely an such asynchronous API, but synchronously calls the
resolve
callback. That means that when the constructed promise object is assigned to yourpromise
variable, that promise is already in a fulfilled state.