javascript - Promise.all 中的 Thenables(ES6 和 Bluebird)

标签 javascript promise bluebird es6-promise

这个安全装置有必要吗?

Promise.all([...].map((thenable) => Promise.resolve(thenable)));

是否可以安全地将 thenable 提供给收集方法 - Promise.allPromise.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/

相关文章:

javascript - 样式 Angular Material 导航栏

javascript - 如何将回复注入(inject)推特的回复部分?

javascript - 将 Select 2 与 ASP.NET MVC 结合使用

node.js - Nodejs Bluebird promise 在处理图像时失败

node.js - 通过 Bluebird promise aws-sdk

javascript - Mocha 单元测试不调用对象内的函数生成器

promise - 如何从 promise 中退出 promise ?

javascript - 解析函数并使用其副作用在服务中记录数据

JavaScript 和 Promise : how to refactor/flatten the following nested promises?

javascript - Bluebird 的 promise 和领域