是否每个ECMAScript运行时都在等待thenable? [英] Does every ECMAScript runtime with await await any Thenable?

查看:124
本文介绍了是否每个ECMAScript运行时都在等待thenable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下当前在节点,chrome,firefox中记录Yahtzee.

The following currently logs Yahtzee in node, chrome, firefox.

如您所见,甚至没有设置Promise的原型.

As you can see, not even prototype of Promise was set.

const fake = new Number(1)
fake.then = fn => setTimeout(fn, 0, 'Yahtzee')
const main = async () => {
  console.log(await fake)
}
main()

这是否通用? 更重要的是,这种行为可能会持续吗?

Does this works universally? And more importantly, will this behavior likely persist?

推荐答案

是的,这将起作用. 根据规范,将调用一个新的Promise并使用该值进行解析. Promise.resolve与thenable一起使用,因此对await

Yes, this will work. According to the specs, a new Promise will be called and resolve with that value. Promise.resolve works with thenable so it will work the same for await

所以,以上等同于

const fake = new Number(1)
fake.then = fn => setTimeout(fn, 0, 'Yahtzee')
const main = async () => {
  console.log(await Promise.resolve(fake))
}
main()

.resolve()在对象上调用then 的方法.

Where it is the .resolve() method that calls then on the object.

这篇关于是否每个ECMAScript运行时都在等待thenable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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