Skip to content

Function: fromPromise()

fromPromise<T>(promise): Stream<T>

Defined in: streams/fromPromise.ts:14

Creates a stream that emits the resolved value of a Promise and then completes.

This is a simple but powerful operator for converting a single, asynchronous value into a stream. If the promise is rejected, the stream will emit an error. The stream will emit exactly one value before it completes.

Type Parameters

T

T = any

The type of the value that the promise resolves to.

Parameters

promise

Promise<T>

The promise to convert into a stream.

Returns

Stream<T>

A new stream that emits the resolved value of the promise.

Examples

From fromPromise.spec.ts:4

typescript
const value = 'test_value';
const promise = Promise.resolve(value);
const stream = fromPromise(promise);
const emittedValues: any[] = [];
stream.subscribe({
  next: (value: any) => emittedValues.push(value),
  complete: () => {
    expect(emittedValues).toEqual([value]);
    done();
  },
});

Released under the MIT License.