method Stream.recoverWhen
Stream.recoverWhen<
E2 extends E,
U,
>
(
guard: (error: E) => error is E2,
fn: (error: E2) => Promisable<U>,
): Stream<T | U, Exclude<E, E2>>

Recovers from specific error types by applying the provided function to transform them into successful values. This allows you to handle specific errors gracefully while still collecting other errors in the stream.

Examples

Example 1

import { Stream } from "anabranch";

const stream = Source.from<number, "aaaah!" | "eeeek!">(async function* () {
  yield 1;
  yield 2;
});

const recoveredStream = stream.recoverWhen(e => e === "aaaah!" as const, e => 42);

Type Parameters

E2 extends E

Parameters

guard: (error: E) => error is E2
fn: (error: E2) => Promisable<U>

Return Type

Stream<T | U, Exclude<E, E2>>

Usage

import { type Stream } from ".";