runGenerator
@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 |
|---|
|
|
Parameters
| Parameter | Type |
|---|---|
| () => |
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 |
|---|
|
|
|
Parameters
| Parameter | Type | Description |
|---|---|---|
| () => | A generator function that |
| ( | Converts a thrown value into an expected error of type |
Returns
Result<InferOk<R>, MergeErr<MergeErr<InferErr<Y>, InferErr<R>>, E>>