Evercatch - v0.5.0
    Preparing search index...

    Function safeAsync

    • Wraps a Promise in a try-catch block to safely execute it and returns a ResultAsync.

      Type Parameters

      • T

        The type of the success value.

      • E extends string = string

        A string literal type for the error label.

      Parameters

      • promise: Promise<T>

        The promise to execute safely.

      • label: E

        The error label to use if the promise rejects.

      Returns ResultAsync<T, E, unknown>

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

      const [error, value] = await safeAsync(fetch('https://api.example.com/data'), 'fetch_error');
      if (error) {
      console.error(error.source);
      } else {
      console.log(value);
      }