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