Evercatch - v0.5.0
    Preparing search index...

    Function fromAsyncThrowable

    • Converts an async function that may throw an error into a function that returns a ResultAsync.

      Type Parameters

      • A extends any[]

        The arguments of the function.

      • T

        The return type of the function.

      • E extends string

        A string literal type for the error label.

      Parameters

      • fn: (...args: A) => Promise<T>

        The async function that may throw an error.

      • label: E

        The error label to use if the function throws.

      Returns ResultAsyncFn<A, T, E, unknown>

      A new async function that wraps the original function and returns a ResultAsync.

      const safeFetch = fromAsyncThrowable(fetch, 'fetch_error');
      const [error, response] = await safeFetch('https://api.example.com/data');
      if (error) {
      // Handle fetch error
      } else {
      // Use response
      }