Tweakr
    Preparing search index...

    Function throttleAsync

    • Creates a throttled version of an asynchronous function.

      Ensures that the function is invoked at most once per specified interval. Subsequent calls within the interval return the pending promise from the previous call.

      Type Parameters

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

        The async function type.

      Parameters

      • fn: T

        The asynchronous function to throttle.

      • interval: number

        Minimum time in milliseconds between function invocations.

      Returns (...args: Parameters<T>) => Promise<any>

      A throttled async function with the same signature as fn.

      const fetchData = async (id: number) => {
      console.log("Fetching", id);
      return id;
      };

      const throttled = throttleAsync(fetchData, 1000);
      await throttled(1); // Executes
      await throttled(2); // Returns previous pending promise

      1.1.0