Tweakr
    Preparing search index...

    Function debounceAsync

    • Creates a debounced version of an asynchronous function with cancellation support.

      Only the last call within the specified delay is executed. All previous pending promises are rejected with a "Cancelled due to debounce" error.

      Type Parameters

      • T extends (...args: any[]) => Promise<any>

        Type of the async function to debounce.

      Parameters

      • fn: T

        The async function to debounce.

      • delay: number

        Milliseconds to wait before executing the function.

      Returns { cancel(): void; (...args: Parameters<T>): Promise<ReturnType<T>> }

      Debounced async function with .cancel() method.

      const fetchData = async (q: string) => `Result for ${q}`;
      const debouncedFetch = debounceAsync(fetchData, 300);

      debouncedFetch("apple");
      debouncedFetch("banana"); // cancels "apple"

      await debouncedFetch("cherry");
      // → "Result for cherry"

      1.2.0