Skip to content

Variable.tryCatchAsync

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

resultar / tryCatchAsync

Variable: tryCatchAsync()

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

Defined in: result-async.ts:478

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

Type Parameters

T

T

E

E

Parameters

fn

The Promise or a function returning a Promise to be wrapped in a ResultAsync.

Promise<T> | () => Promise<T>

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