Evercatch - v0.9.0
    Preparing search index...

    Function resultFrom

    • 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.

      • mapErr: (err: unknown) => E = defaultErrorMapper

        A function to map errors to a specific type.

      Returns Result<T, E>

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

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