此代码适用于:
Promise.all([Promise.resolve(1)])但是,此代码没有:
const f = Promise.all
f([Promise.resolve(1)])第二段代码引发以下错误:Uncaught TypeError: Promise.all called on non-object。
为什么我不能将函数Promise.all赋值给一个变量并正常使用它呢?
发布于 2022-06-04 02:11:25
因为这就是规格要求。
1. Let C be the this value.
2. Let promiseCapability be ? NewPromiseCapability(C).
3. Let promiseResolve be Completion(GetPromiseResolve(C)).
...all函数要求它的这个值是一个构造函数,它支持允诺构造函数的参数约定。
当前的代码没有将函数作为对象的一部分调用(也就是说,f是一个独立的标识符),因此得到的this要么是全局对象,要么是undefined,这取决于您是否处于严格模式--无论是哪种方式,都没有满足上述要求。
如果你把函数绑定到允诺,它就会起作用。
const f = Promise.all.bind(Promise);https://stackoverflow.com/questions/72496593
复制相似问题