Evercatch - v0.3.1
    Preparing search index...

    Function fromThrowable

    • Wraps a function that may throw into a function that returns a Result.

      Type Parameters

      • A extends any[]
      • T
      • E extends string

      Parameters

      • fn: (...args: A) => T

        The function to wrap that may throw an error

      • label: E

        A label to identify the error when the function throws

      Returns ResultFn<A, T, E>

      A function that returns a Result

      const safeJSONParse = fromThrowable(JSON.parse, 'PARSE_ERROR');
      const success = safeJSONParse('{"foo": "bar"}'); // [null, { foo: "bar" }]
      const fail = safeJSONParse('invalid json'); // [Err<'PARSE_ERROR'>, undefined]