Skip to content

Function.tryCatchAsync

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

resultar / tryCatchAsync

Function: tryCatchAsync()

tryCatchAsync<T, E>(fn, errorFn?): ResultAsync<T, E>

Defined in: result-async.ts:474

Creates a ResultAsync from a Promise, catching any errors that occur during its execution.

Type Parameters

T

E

Parameters

fn

Promise<T>

The Promise to be wrapped in a ResultAsync.

errorFn?

(e) => E

Optional function to transform the caught error into a specific error type. If not provided, the original error will be used.

Returns

ResultAsync<T, E>

A ResultAsync that will resolve to Ok with the promise's value if successful, or Err with either the transformed error (if errorFn is provided) or the original error.

Example

// Without error transformer
const result = ResultAsync.tryCatch(Promise.resolve(42));

// With error transformer
const result = ResultAsync.tryCatch(
  fetch('https://api.example.com'),
  (error) => new CustomError('API request failed')
);

Clone this wiki locally