method Stream.flatMap
Stream.flatMap<U>(fn: (value: T) => Promisable<AsyncIterable<U> | Iterable<U>>): Stream<U, E>

Similar to Array.prototype.flatMap, but works on the stream of results. If the provided function throws an error or returns a rejected promise, the error will be collected and emitted as an error result in the stream.

When concurrency is greater than 1, results may be emitted out of order.

Examples

Example 1

import { Source } from "anabranch";

const stream = Source.from<number, string>(async function* () {
  yield 1;
  yield 2;
});

const flattened = stream.flatMap((value) => [value, value * 10]);

Type Parameters

Parameters

fn: (value: T) => Promisable<AsyncIterable<U> | Iterable<U>>

Return Type

Stream<U, E>

Usage

import { type Stream } from ".";