Skip to content

tryCatch

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

Defined in: Core/TaskValidation.ts:58

Creates a TaskValidation from a Promise-returning function. Catches any errors and transforms them using the onError function.

E

A

() => Promise<A>

(e) => E

TaskValidation<E, A>

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