method Stream.scanErr
Stream.scanErr<F>(
fn: (
acc: F,
error: E,
errorIndex: number
) => Promisable<F>
,
initialValue: F
): Stream<T, F>

Like scan but works on the stream of errors. Emits the running accumulator after each error, allowing downstream operations to react to intermediate error states. If the provided function throws an error or returns a rejected promise, the new error will be collected and emitted as an error result in the stream.

Examples

Example 1

import { Source } from "anabranch";

const stream = Source.from<number, string>(async function* () {
  yield 1;
  throw new Error("First error");
  yield 2;
  throw new Error("Second error");
});

const errorScan = stream.scanErr((acc, err) => {
  return acc ? `${acc}; ${err.message}` : err.message;
}, "");

for await (const result of errorScan) {
  if (result.type === "error") {
    console.error("Accumulated error:", result.error);
  }
}

Type Parameters

Parameters

fn: (
acc: F,
error: E,
errorIndex: number
) => Promisable<F>
initialValue: F

Return Type

Stream<T, F>

See

Usage

import { type Stream } from ".";