Evercatch - v0.9.0
    Preparing search index...

    Function fromPromise

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

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

        A function to map errors to a specific type.

      Returns ResultAsync<T, E>

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

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