method Stream.throwOn
Stream.throwOn<E2 extends E>(guard: (error: E) => error is E2): Stream<T, Exclude<E, E2>>

Throws the specified error types if they are encountered in the stream. This allows you to handle specific errors immediately while continuing to process other errors.

Examples

Example 1

import { Stream } from "anabranch";

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

const streamWithThrownErrors = stream.throwOn(e => e === "eeeek!");

try {
  for await (const value of streamWithThrownErrors.successes()) {
    console.log("Value:", value);
  }
} catch (error) {
  console.error("Caught error:", error); // This will catch "eeeek!" errors thrown by throwOn
}

Type Parameters

E2 extends E

Parameters

guard: (error: E) => error is E2

Return Type

Stream<T, Exclude<E, E2>>

Usage

import { type Stream } from ".";