Skip to main content

runAsyncGenerator

@ts-rust/std


@ts-rust/std / Result / runAsyncGenerator

Function: runAsyncGenerator()

Call Signature​

function runAsyncGenerator<Y, R>(
action,
): PendingResult<InferOk<R>, MergeErr<InferErr<Y>, InferErr<R>>>;

Defined in: packages/std/src/result/result.ts:353

Evaluates an async generator that yield*s Result or PendingResult values, returning a PendingResult for the first yielded Err or the Result the generator returns.

This is the asynchronous counterpart of runGenerator. yield* unwraps Ok into the contained value, or aborts with Err.

The function itself is synchronous and returns a PendingResult immediately. The async generator is driven internally; await the returned pending result (or use PendingResult.then) to get the Result. The pending result's promise is always handled, so not awaiting it does not produce an unhandled rejection.

Thrown exceptions and rejected yield*s become UnexpectedError, unless mkErr is provided to map them into an expected error (same role as runAsync's mkErr).

Type Parameters​

Type Parameter

Y

R extends Result<unknown, unknown>

Parameters​

ParameterType

action

() => AsyncGenerator<Y, R>

Returns​

PendingResult<InferOk<R>, MergeErr<InferErr<Y>, InferErr<R>>>

Example​

const result = await runAsyncGenerator(async function* () {
const a = yield* pendingOk<number, string>(1);
const b = yield* ok<number, string>(2);
return ok(a + b);
});

expect(result).toStrictEqual(ok(3));

Call Signature​

function runAsyncGenerator<Y, R, E>(
action,
mkErr,
): PendingResult<InferOk<R>, MergeErr<MergeErr<InferErr<Y>, InferErr<R>>, E>>;

Defined in: packages/std/src/result/result.ts:364

Evaluates an async generator that yield*s Result or PendingResult values, mapping thrown exceptions through mkErr.

Type Parameters​

Type Parameter

Y

R extends Result<unknown, unknown>

E

Parameters​

ParameterTypeDescription

action

() => AsyncGenerator<Y, R>

An async generator function that yield*s Result or PendingResult values and returns a Result.

mkErr

(error) => Awaited<E>

Converts a thrown value into an expected error of type E.

Returns​

PendingResult<InferOk<R>, MergeErr<MergeErr<InferErr<Y>, InferErr<R>>, E>>