简体   繁体   中英

Thenables in Promise.all (ES6 and Bluebird)

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM