5 comments

[ 1.6 ms ] story [ 19.4 ms ] thread
I wonder why Promise.all is not a language construct but a regular method call bringing Promise object out of nowhere.

It would've been easier if await just accepted arrays just like how generators in co could use yield on arrays to wait on them all.

Also when do you need to use Promise.race?

await does "accept" arrays, it resolves to the array you pass it
It's just returning it as is right?
You can't await something that isn't a promise like an array. You should get a TypeError.
Promise.all is one among potentially many Promise combinators someone may wish to use with an array of Promises. (Promise.race is another. There are others, especially as you expand out into the Observable space.)

One example for Promise.race might be a Promise to get data from a cache and another from an HTTP call and you don't care which returns the data, you just want whichever is fastest. Your HTTP call might eventually update your cache and "repaint/recalculate" somehow down the line. That HTTP call still might be "faster" than your cache if the cache hasn't been populated yet and is waiting for data to arrive.