Skip to main content

runGenerator

@ts-rust/std


@ts-rust/std / Result / runGenerator

Function: runGenerator()

Call Signature

function runGenerator<Y, R>(
action,
): Result<InferOk<R>, MergeErr<InferErr<Y>, InferErr<R>>>;

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

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

This emulates Rust's ? operator. yield* someResult unwraps Ok into the contained value, or aborts with Err.

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

Async generators are not accepted; use runAsyncGenerator instead.

Type Parameters

Type Parameter

Y

R extends Result<unknown, unknown>

Parameters

ParameterType

action

() => Generator<Y, R>

Returns

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

Example

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

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

Call Signature

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

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

Evaluates a generator that yield*s Result values, mapping thrown exceptions through mkErr.

Type Parameters

Type Parameter

Y

R extends Result<unknown, unknown>

E

Parameters

ParameterTypeDescription

action

() => Generator<Y, R>

A generator function that yield*s Result values and returns a Result.

mkErr

(error) => Awaited<E>

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

Returns

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