Skip to content

Function.safeTry

GitHub Actions edited this page Oct 24, 2024 · 23 revisions

resultar / safeTry

Function: safeTry()

safeTry(body)

safeTry<T, E>(body): Result<T, E>

Evaluates the given generator to a Result returned or an Err yielded from it, whichever comes first.

This function, in combination with Result.safeUnwrap(), is intended to emulate Rust's ? operator. See /tests/safeTry.test.ts for examples.

Type Parameters

T

E

Parameters

body

What is evaluated. In body, yield* result.safeUnwrap() works as Rust's result? expression.

Returns

Result<T, E>

The first occurence of either an yielded Err or a returned Result.

Defined in

result.ts:521

safeTry(body)

safeTry<YieldErr, GeneratorReturnResult>(body): Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>

Type Parameters

YieldErr extends Result<never, unknown>

GeneratorReturnResult extends Result<unknown, unknown>

Parameters

body

Returns

Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>

Defined in

result.ts:522

safeTry(body)

safeTry<T, E>(body): ResultAsync<T, E>

Evaluates the given generator to a Result returned or an Err yielded from it, whichever comes first.

This function, in combination with Result.safeUnwrap(), is intended to emulate Rust's ? operator. See /tests/safeTry.test.ts for examples.

Type Parameters

T

E

Parameters

body

What is evaluated. In body, yield* result.safeUnwrap() and yield* resultAsync.safeUnwrap() work as Rust's result? expression.

Returns

ResultAsync<T, E>

The first occurence of either an yielded Err or a returned Result.

Defined in

result.ts:544

safeTry(body)

safeTry<YieldErr, GeneratorReturnResult>(body): ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>

Type Parameters

YieldErr extends Result<never, unknown>

GeneratorReturnResult extends Result<unknown, unknown>

Parameters

body

Returns

ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>

Defined in

result.ts:547