Skip to content

Function: eachValueFrom()

eachValueFrom<T>(stream): AsyncGenerator<T>

Defined in: converters/eachValueFrom.ts:26

Converts a Stream into an async generator, yielding each emitted value. Distinguishes between undefined values and stream completion.

This function creates a bridge between the push-based nature of a stream and the pull-based nature of an async generator. It subscribes to the stream and buffers incoming values in a queue. When the generator is iterated over (e.g., in a for await...of loop), it first yields any buffered values before asynchronously waiting for the next value to be pushed.

The generator handles all stream events:

  • Each yielded value corresponds to a next event, including undefined values.
  • The generator terminates when the stream completes.
  • It throws an error if the stream emits an error event.

It correctly handles situations where the stream completes or errors out before any values are yielded, and ensures the subscription is always cleaned up.

Type Parameters

T

T = any

The type of the values emitted by the stream.

Parameters

stream

Stream<T>

The source stream to convert.

Returns

AsyncGenerator<T>

An async generator that yields the values from the stream.

Examples

From eachValueFrom.spec.ts:4

typescript
const first = await firstValueFrom(from([1, 2, 3]));
expect(first).toBe(1);

Released under the MIT License.