Skip to content

tryCatch

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

Defined in: Core/TaskValidation.ts:101

Creates a Task.Validation from a Promise-returning function. 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

TaskValidation<E, A>

const fetchUser = (id: string): Task.Validation<string, User> =>
  Task.Validation.tryCatch(
    (signal) => fetch(`/users/${id}`, { signal }).then(r => r.json()),
    e => `Failed to fetch user: ${e}`
  );