Evercatch - v0.5.0
    Preparing search index...

    Function safe

    • Wraps a function in a try-catch block to safely execute it and returns a Result.

      Type Parameters

      • T

        The type of the success value.

      • E extends string

        A string literal type for the error label.

      Parameters

      • fn: () => T

        The function to execute safely.

      • label: E

        The error label to use if the function throws an error.

      Returns Result<T, E, unknown>

      A Result containing either the success value or a structured error.

      const [error, value] = safe(() => JSON.parse('{"foo": "bar"}'), 'json_parse_error');
      if (error) {
      console.error(error.source);
      } else {
      console.log(value);
      }