Skip to content

Variable.tryCatch

GitHub Actions edited this page May 22, 2025 · 1 revision

resultar / tryCatch

Variable: tryCatch()

const tryCatch: <T, E>(fn, errorFn?) => Result<T, E> = Result.tryCatch

Defined in: result.ts:630

Creates a Result by running a function that might throw. If the function throws, the error will be caught and returned as an Err. If the function succeeds, the value will be wrapped in an Ok.

Type Parameters

T

T

E

E

Parameters

fn

() => Exclude<T, Promise<unknown>>

The function to execute, which might throw an error

errorFn?

(e) => E

Optional function to transform the thrown error before wrapping in Err

Returns

Result<T, E>

A Result containing either the function's return value or the caught error

Clone this wiki locally