werkbank
    Preparing search index...

    Function useAsyncCallback

    • Wraps an async function with loading and error state, and handles cancellation.

      Type Parameters

      • Args extends any[]

        The arguments of the async function.

      • Error = unknown

        The error type.

      Parameters

      • fn: AsyncCallback<Args>

        The async function to wrap.

      Returns UseAsyncCallback<Args, Error>

      A tuple [callback, state].

      The callback receives an AbortSignal as its last argument. Previous executions are aborted when a new one starts.

      const [fetchData, { loading, error }] = useAsyncCallback(async (id: string, signal) => {
      const response = await fetch(`/api/${id}`, { signal });
      // ...
      });