Extend `Promise.all` support `async iterator`.

Iterator can using Array.from expansion,

But AsyncIterator no way to expansion to array.

Make Promise.all support AsyncIterator input.

Can avoid implementing it yourself

async function * foo() {
    yield 1
    yield 2
    yield 3
    yield 4
}

const result = await Promise.all(foo());
// Output: [1, 2, 3, 4]

I think Promise.all should stay reserved for the use case of watching a known set of promises to become fulfilled, it should not be extended to support dynamically growing sets. I'd assume this would also break compatibility for objects that are both synchronously and asynchronously iterable.

As a better solution, the iterator helpers proposal already contains a AsyncIterator.prototype.toArray method which can be used as

const result = await foo().toArray();
3 Likes