Skip to content

tryCatch

tryCatch<E, A>(f, onError): TaskResult<E, A>

Defined in: Core/TaskResult.ts:85

Creates a Task.Result from a function that may throw. Catches any errors and transforms them using the onError function. The factory optionally receives an AbortSignal forwarded from the call site.

E

A

(signal?) => Thenable<A>

(e) => E

TaskResult<E, A>

const fetchUser = (id: string): Task.Result<string, User> =>
  Task.Result.tryCatch(
    (signal) => fetch(`/users/${id}`, { signal }).then(r => r.json()),
    String
  );