Skip to content

Function.tryCatch

GitHub Actions edited this page Mar 5, 2025 · 4 revisions

resultar / tryCatch

Function: tryCatch()

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

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

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