@jiminp/tooltool
    Preparing search index...

    Function rateLimited

    • Serializes async calls in FIFO order with a minimum delay between their start times.

      Useful for throttling API calls without overlapping requests.

      Type Parameters

      • ArgsType extends unknown[]

        Tuple of the function's parameter types.

      • T

        The resolved return type.

      Parameters

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

        The async function to rate-limit.

      • duration_ms: number | (() => number)

        Minimum delay (ms), or a function returning the delay dynamically.

      Returns RateLimitedFunction<ArgsType, T>

      A RateLimitedFunction.

      A call waits for both the previous call to settle and its start-to-start cooldown, including when the queue was empty between calls. The first call has no cooldown. Dynamic durations are sampled when scheduling queued work and again when a timer fires, not continuously; changing the duration does not wake an existing timer early. Timer precision and maximum supported delays are platform-dependent.

      const limitedFetch = rateLimited(fetchJson, 500);
      await limitedFetch("/endpoint"); // executes immediately
      await limitedFetch("/endpoint"); // starts ≥500ms after the previous start