Skip to content

Function: from()

from<T>(source): Stream<T>

Defined in: streams/from.ts:15

Creates a stream from an asynchronous or synchronous iterable.

This operator is a powerful way to convert any source that can be iterated over (such as arrays, strings, Map, Set, AsyncGenerator, etc.) into a reactive stream. The stream will emit each value from the source in order before completing.

Type Parameters

T

T = any

The type of the values in the iterable.

Parameters

source

The iterable source to convert into a stream.

AsyncIterable<T> | Iterable<T>

Returns

Stream<T>

A new stream that emits each value from the source.

Examples

From from.spec.ts:5

typescript
const values = [1, 2, 3];
const stream = from(values);
let emittedValues: any[] = [];
await new Promise<void>((resolve, reject) => {
  stream.subscribe({
    next: (value) => emittedValues.push(value),
    complete: () => {
      try {
        expect(emittedValues).toEqual(values);
        resolve();
      } catch (e) {
        reject(e);
      }
    },
    error: reject,
  });
});

Released under the MIT License.