Evercatch - v0.8.0
    Preparing search index...

    Function safe

    • Safely executes a function, catching any errors.

      Type Parameters

      • T

        The return type of the function.

      • E = Error

        The type of the error.

      Parameters

      • fn: () => T

        The function to execute.

      • Optionaloptions: Options<E>

        Options for error handling.

      Returns Result<T, E>

      A Result containing either the function's return value or the caught error.

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