@vgerbot/async
    Preparing search index...

    Function throttle

    • Creates a throttled version of an async function. The function will execute at most once per wait period, regardless of how many times it's called.

      Type Parameters

      • T

        The return type of the function.

      • Args extends unknown[] = unknown[]

      Parameters

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

        The async function to throttle.

      • wait: number

        Minimum milliseconds between executions.

      • Optionaloptions: ThrottleOptions

        Throttle configuration options.

      Returns ThrottledFunction<T, Args>

      A throttled function with cancel, flush, and pending methods.

      const saveData = throttle(
      async (data: string) => {
      return await api.save(data);
      },
      1000,
      );

      saveData("a"); // Executes immediately
      saveData("b"); // Queued, executes after 1000ms
      saveData("c"); // Replaces "b", executes after 1000ms

      // Cancel pending execution
      saveData.cancel();

      // Execute immediately
      saveData.flush();