runPendingResult
@ts-rust/std / Result / runPendingResult
Function: runPendingResult()
function runPendingResult<T, E>(getResult): PendingResult<T, E>;
Defined in: packages/std/src/result/result.ts:329
Safely executes an action that returns a PendingResult, capturing thrown synchronous errors as an Err variant.
The runPendingResult function executes the provided resultAction
function, which returns a PendingResult<T, E>
. If the action succeeds, it
returns the PendingResult as-is. If the action throws an error synchronously,
the error is captured and wrapped in a resolved Err variant returning
UnexpectedError with a ResultErrorKind.Unexpected
kind.
This overload is useful for safely running asynchronous PendingResult
-producing actions,
if you are not 100% sure that the action will not throw an error, ensuring that any
synchronous errors are converted into an Err variant in a type-safe way.
Type Parameters
Type Parameter |
---|
|
|
Parameters
Parameter | Type | Description |
---|---|---|
| () => | | A function that returns a |
Returns
PendingResult
<T
, E
>
A PendingResult<T, E>
containing either the original PendingResult
from resultAction
or a resolved Promise
with an Err<E>
if the action throws synchronously.
Example
import { runPendingResult, pendingOk, pendingErr } from "@ts-rust/std";
// Successful Result
const success = await runPendingResult(() => pendingOk(42));
console.log(success.unwrap()); // 42
// Failed Result
const failure = await runPendingResult(() =>
pendingErr(new Error("Already failed")),
);
// "Expected error occurred: Error: Already failed"
console.log(failure.unwrapErr().expected?.message);
// Action throws an error
const thrown = await runPendingResult(() => {
throw new Error("Oops");
});
// "Unexpected error occurred: ResultError: [Unexpected] `runPendingResult`: result action threw an exception. Reason: Error: Oops"
console.log(thrown.unwrapErr().unexpected?.message);