Skip to content

throttleId ​

Throttle an async function with isolated state per identifier — runs immediately, then at most once per interval.

throttleId is the async, keyed counterpart of throttle. The first call runs immediately and returns its promise; further calls within interval are coalesced and resolve to the result of the last replayed call.

Usage ​

ts
import { throttleId } from '@maz-ui/utils'

const trackEvent = throttleId(
  'analytics',
  async (event: { name: string }) => {
    return fetch('/track', { method: 'POST', body: JSON.stringify(event) })
  },
  1000,
)

trackEvent({ name: 'scroll' })
trackEvent({ name: 'scroll' }) // coalesced into the first call

API ​

ts
function throttleId<T, Args extends unknown[]>(
  identifier: string,
  func: (...args: Args) => T | Promise<T>,
  interval: number,
): (...args: Args) => Promise<T>
ParameterTypeDescription
identifierstringUnique key isolating this throttle from others
funcFunctionThe async (or sync) function to throttle
intervalnumberMinimum interval between executions, in milliseconds
  • debounceId — wait for silence instead of rate-limiting.
  • throttle — non-async, single-instance throttle.