API · Transform

collect()

Buffer all successful values and emit one array when the source ends.

Example

const [records] = await exstream(source).collect().toArray()

Behavior

collect() preserves input order and emits exactly one successful value at normal source completion. An empty source emits an empty array:

await exstream([]).collect().toArray()
// [[]]

It is an intermediate operator, not a terminal consumer. The returned stream still needs demand from toArray(), drain(), pipeTo(), or another consumer.

Buffering

collect() must retain every successful value because its only output is one complete array. It cannot emit that array until the source ends, so memory use grows linearly with input size and has no built-in limit.

Use it only when the maximum input size is known and acceptable. Prefer batch() when the work can be processed incrementally.

Context

The emitted array has an aggregate context. Its contexts property preserves materialized input contexts in input order, and its input is the collected array.

Errors

Record errors pass through immediately and are excluded from the array. If handled downstream, collection continues and the array is still emitted at normal completion. An unhandled or fatal failure prevents normal completion and therefore prevents the final array from being emitted.

Forms

collect() is available on streams and reusable pipelines:

stream.collect()
exstream.pipeline().collect()

Signature

collect(): Exstream<T[], AggregateOutputContext<C, T[]>>

batch(), drain(), async iteration