Evercatch - v0.8.0
    Preparing search index...

    Function fromAsyncThrowable

    • Wraps an async function to return a ResultAsync instead of throwing errors.

      Type Parameters

      • F extends AnyAsyncFunction

        The async function type.

      • E = Error

        The type of the error.

      Parameters

      • fn: F

        The async function to wrap.

      • Optionaloptions: Options<E>

        Options for error handling.

      Returns ResultAsyncFn<F, E>

      An async function that returns a ResultAsync.

      const safeFetch = fromAsyncThrowable(fetch);

      const [error, response] = await safeFetch("https://api.example.com/data");
      if (error) {
      console.error(error.message);
      } else {
      const data = await response.json();
      console.log(data);
      }