method StreamClass.unfold
Private
StreamClass.unfold<S, U>(
seed: S,
next: (state: S) =>
Promise<readonly [U, S | null] | null>
| readonly [U, S | null]
| null
): StreamClass<Exclude<U, AnyFailure>, Extract<U, AnyFailure>>

Elixir's Stream.unfold/2: grow a lazy stream from a seed. next(state) returns [element, nextState] to emit and continue, or null to end; return [failure, null] to emit a declared failure and stop. Nothing runs until a consumer pulls — pagination only fetches the pages the consumer actually reaches.

const countdown = Stream.unfold(3, (n) => (n === 0 ? null : [n, n - 1] as const));
await countdown.collect(); // [3, 2, 1]

Type Parameters

Parameters

seed: S
next: (state: S) =>
Promise<readonly [U, S | null] | null>
| readonly [U, S | null]
| null

Return Type

StreamClass<Exclude<U, AnyFailure>, Extract<U, AnyFailure>>

Usage

import { StreamClass } from "lib/stream/stream.ts";