@jiminp/tooltool
    Preparing search index...

    Function rateLimited

    • Wraps an asynchronous function so that it is executed at most once every duration_ms milliseconds. Additional calls are queued and processed sequentially following a strict FIFO (first-come first-served) order.

      This is useful when throttling API calls or expensive operations.

      Type Parameters

      • ArgsType extends unknown[]

        Parameter tuple type of the wrapped function.

      • T

        Resolved return type of the wrapped function.

      Parameters

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

        The asynchronous function to rate-limit.

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

        Either a fixed number of milliseconds, or a function that returns the delay dynamically at each scheduling point.

      Returns RateLimitedFunction<ArgsType, T>

      A RateLimitedFunction instance that enforces the rate limit.

      const limitedFetch = rateLimited(fetchJson, 500);

      await limitedFetch("/endpoint"); // executes immediately
      await limitedFetch("/endpoint"); // queued and executed ≥500ms later
      console.log(limitedFetch.wait_count); // number of queued calls