Skip to content

asyncCollect

asyncCollect<T>(xs): Promise<T[]>

Collect the values from an AsyncIterable and return them as an array.

Example

await asyncCollect([1, 2, 3])
// => [2, 3, 4]
await asyncCollect([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)])
// => [2, 3, 4]
await asyncCollect(asyncMap([1, 2, 3], (x) => x + 1))
// => [2, 3, 4]
const asyncFn = <T>(x: T) => Promise.resolve(x)
async function* foobar() {
yield await asyncFn('foo')
yield await asyncFn('Bar')
}
await asyncCollect(asyncMap(foobar(), asyncFn))
// => ["foo", "bar"]

Alternatives

Proposals

Type Parameters

T

The element type.

Parameters

xs

The values to map over.

AsyncIterable<T> | Iterable<T>

Returns

Promise<T[]>

An array with the values from the async generator.