method Stream.map
Stream.map<
U,
E2 = E,
>
(fn: (value: T) => Promisable<U>): Stream<U, E | E2>

Similar to Array.prototype.map, 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 { Stream } from "anabranch";

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

const mappedStream = stream.map(async (value) => {
  if (value === 2) {
    throw new Error("Value cannot be 2");
  }
  return value * 2;
});

Type Parameters

E2 = E

Parameters

fn: (value: T) => Promisable<U>

Return Type

Stream<U, E | E2>

See

Usage

import { type Stream } from ".";