method Stream.foldErr
Stream.foldErr<F>(
fn: (
acc: F,
error: E,
) => Promisable<F>
,
initialValue: F,
): Promise<F>

Similar to Array.prototype.reduce, but works on the stream of errors. 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 { Stream } from "anabranch";

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

const concatenatedErrors = await stream.foldErr(async (acc, error) => {
  return `${acc}; ${error}`;
}, "");

console.log("Concatenated Errors:", concatenatedErrors);

Type Parameters

Parameters

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

Return Type

Promise<F>

See

Usage

import { type Stream } from ".";