Thenables in Promise.all (ES6 and Bluebird)

时间:2016-07-11 19:15:26

标签: javascript promise bluebird es6-promise

Is this safety device necessary?

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

Is it possible and safe to supply thenables to collection methods - Promise.all, Promise.race, etc? Any pitfalls?

The question concerns Bluebird, as well as polyfilled and all native ES6 promise implementations.

1 个答案:

答案 0 :(得分:5)

This is not necessary and should be done by the promise implementation itself:

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 )

Phrased more accessible by MDN:

If something passed in the iterable array is not a promise, it's converted to one by Promise.resolve.