Evercatch - v0.8.0
    Preparing search index...

    Function safeAsync

    • Safely awaits a promise, catching any errors.

      Type Parameters

      • T

        The resolved type of the promise.

      • E = Error

        The type of the error.

      Parameters

      • promise: Promise<T>

        The promise to await.

      • Optionaloptions: Options<E>

        Options for error handling.

      Returns ResultAsync<T, E>

      A Promise of a Result containing either the resolved value or the caught error.

      const [error, data] = await safeAsync(
      fetch("https://api.example.com/data").then((res) => res.json()),
      );
      if (error) {
      console.error(error.message);
      } else {
      console.log(data);
      }