Skip to content

tryCatch

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

Defined in: Core/Result.ts:68

Creates an Result from a function that may throw. Catches any errors and transforms them using the onError function.

E

A

() => A

(e) => E

Result<E, A>

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