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.

E

A

() => Promise<A>

(e) => E

TaskResult<E, A>

const parseJson = (s: string): TaskResult<string, unknown> =>
  TaskResult.tryCatch(
    async () => JSON.parse(s),
    (e) => `Parse error: ${e}`
  );