Skip to main content

pendingResult

@ts-rust/std


@ts-rust/std / Result / pendingResult

Function: pendingResult()

function pendingResult<T, E>(resultOrFactory): PendingResult<T, E>;

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

Creates a PendingResult\<T, E\> from a result, promise, or factory function.

Accepts a Result, a Promise resolving to a Result, or a function returning either, and converts it into a pending result, handling asynchronous resolution as needed.

Type Parameters

Type ParameterDescription

T

The type of the value in the result.

E

The type of the expected error in the result.

Parameters

ParameterTypeDescription

resultOrFactory

| Result<T, E> | Promise<Result<T, E>> | () => Result<T, E> | Promise<Result<T, E>>

The Result, promise, or factory function producing a Result.

Returns

PendingResult<T, E>

A PendingResult resolving to the provided or produced result.

Example

const x = pendingResult(ok<number, string>(42));
const y = pendingResult(() => Promise.resolve(err<string, number>(42)));
const z = pendingResult(async () => err<string, boolean>(true));

expect(await x).toStrictEqual(ok(42));
expect(await y).toStrictEqual(err(42));
expect(await z).toStrictEqual(err(true));