Skip to content

Function: of()

of<T>(value): Stream<T>

Defined in: streams/of.ts:15

Creates a stream that emits a single value and then completes.

This operator is useful for scenarios where you need to treat a static, single value as a stream. It immediately yields the provided value and then signals completion, which is a common pattern for creating a "hot" stream from a predefined value.

Type Parameters

T

T = any

The type of the value to be emitted.

Parameters

value

CallbackReturnType<T>

The single value to emit.

Returns

Stream<T>

A new stream that emits the value and then completes.

Examples

From of.spec.ts:4

typescript
const value = 'test_value';
const ofStream = of(value);
const emittedValues: any[] = [];
ofStream.subscribe({
  next: (v) => emittedValues.push(v),
  complete: () => {
    expect(emittedValues).toEqual([value]);
    done(); // tells Jasmine the async test is done
  },
  error: (err) => done.fail(err),
});

Released under the MIT License.