Evercatch - v0.5.0
    Preparing search index...

    Function fromThrowable

    • Converts a function that may throw an error into a function that returns a Result.

      Type Parameters

      • A extends any[]

        The arguments of the function.

      • T

        The return type of the function.

      • E extends string

        A string literal type for the error label.

      Parameters

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

        The function that may throw an error.

      • label: E

        The error label to use if the function throws.

      Returns ResultFn<A, T, E, unknown>

      A new function that wraps the original function and returns a Result.

      const safeJsonParse = fromThrowable(JSON.parse, 'json_parse_error');
      const [error, data] = safeJsonParse('{"key": "value"}');
      if (error) {
      // Handle parsing error
      } else {
      // Use data
      }