Skip to content

Function: toArray()

toArray<T>(): Operator<T, T[]>

Defined in: operators/toArray.ts:10

Collects all emitted values from the source stream into an array and emits that array once the source completes, tracking pending state.

Type Parameters

T

T = any

The type of the values in the source stream.

Returns

Operator<T, T[]>

An Operator instance for use in a stream's pipe method.

Examples

From toArray.spec.ts:12

typescript
let subject: ReturnType<typeof createSubject<number>> = createSubject<number>();

let source: Stream<number> = subject;

const toArrayStream = source.pipe(toArray());
const results: number[][] = [];
(async () => {
  for await (const value of eachValueFrom(toArrayStream)) {
    results.push(value);
  }
})();
subject.next(1);
subject.next(2);
subject.next(3);
subject.complete();
await new Promise((resolve) => setTimeout(resolve, 100));
expect(results).toEqual([[1, 2, 3]]);

Released under the MIT License.