Skip to content

collect

function collect<T>(source): Promise<T[]>;

Defined in: collect.ts:26

Collect all values from an iterable into an array.

Sync/async adaptive: an AsyncIterable returns a Promise<T[]>, a sync Iterable returns T[] directly — no await needed (and await still works on both).

Type Parameter
T
Parameter Type
source AsyncIterable<T>

Promise<T[]>

Async source

async function* gen() {
yield 1;
yield 2;
}
const values = await collect(gen()); // [1, 2]

Sync source — result is immediate

const values = collect(new Set([1, 2])); // [1, 2], no await
function collect<T>(source): T[];

Defined in: collect.ts:27

Collect all values from an iterable into an array.

Sync/async adaptive: an AsyncIterable returns a Promise<T[]>, a sync Iterable returns T[] directly — no await needed (and await still works on both).

Type Parameter
T
Parameter Type
source Iterable<T>

T[]

Async source

async function* gen() {
yield 1;
yield 2;
}
const values = await collect(gen()); // [1, 2]

Sync source — result is immediate

const values = collect(new Set([1, 2])); // [1, 2], no await
function collect<T>(source): MaybePromise<T[]>;

Defined in: collect.ts:28

Collect all values from an iterable into an array.

Sync/async adaptive: an AsyncIterable returns a Promise<T[]>, a sync Iterable returns T[] directly — no await needed (and await still works on both).

Type Parameter
T
Parameter Type
source AsyncIterable<T, any, any> | Iterable<T, any, any>

MaybePromise<T[]>

Async source

async function* gen() {
yield 1;
yield 2;
}
const values = await collect(gen()); // [1, 2]

Sync source — result is immediate

const values = collect(new Set([1, 2])); // [1, 2], no await