这个安全装置有必要吗?
Promise.all([...].map((thenable) => Promise.resolve(thenable)));
是否可以安全地将 thenable 提供给收集方法 - Promise.all
、Promise.race
等?有什么陷阱吗?
问题涉及 Bluebird,以及 polyfilled 和所有原生 ES6 promise 实现。
最佳答案
这不是必需的,应该由 promise 实现本身来完成:
The
all
function returns a new promise which is fulfilled with an array of fulfillment values for the passed promises, or rejects with the reason of the first passed promise that rejects. It resolves all elements of the passed iterable to promises as it runs this algorithm.
ES2015 Specification, paragraph 25.4.4.1 Promise.all ( iterable )
措辞更易于理解 by MDN :
If something passed in the iterable array is not a promise, it's converted to one by
Promise.resolve
.
关于javascript - Promise.all 中的 Thenables(ES6 和 Bluebird),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314489/