Skip to content

tryCatch

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

Defined in: Core/TaskResult.ts:44

Creates a TaskResult 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?) => Promise<A>

(e) => E

TaskResult<E, A>

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