Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference

AsyncRateLimiter

Class: AsyncRateLimiter<TFn>

Defined in: async-rate-limiter.ts:127

A class that creates an async rate-limited function.

Rate limiting is a simple approach that allows a function to execute up to a limit within a time window, then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where all executions happen immediately, followed by a complete block.

The rate limiter supports two types of windows:

  • 'fixed': A strict window that resets after the window period. All executions within the window count towards the limit, and the window resets completely after the period.
  • 'sliding': A rolling window that allows executions as old ones expire. This provides a more consistent rate of execution over time.

Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function, making it ideal for API calls and other async operations where you want the result of the maybeExecute call instead of setting the result on a state variable from within the rate-limited function.

For smoother execution patterns, consider using:

  • Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)
  • Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)

Rate limiting is best used for hard API limits or resource constraints. For UI updates or smoothing out frequent events, throttling or debouncing usually provide better user experience.

Error Handling:

  • If an onError handler is provided, it will be called with the error and rate limiter instance
  • If throwOnError is true (default when no onError handler is provided), the error will be thrown
  • If throwOnError is false (default when onError handler is provided), the error will be swallowed
  • Both onError and throwOnError can be used together - the handler will be called before any error is thrown
  • The error state can be checked using the underlying AsyncRateLimiter instance
  • Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the onReject handler

Example

ts
const rateLimiter = new AsyncRateLimiter(
  async (id: string) => await api.getData(id),
  {
    limit: 5,
    window: 1000,
    windowType: 'sliding',
    onError: (error) => {
      console.error('API call failed:', error);
    },
    onReject: (limiter) => {
      console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);
    }
  }
);

// Will execute immediately until limit reached, then block
// Returns the API response directly
const data = await rateLimiter.maybeExecute('123');
const rateLimiter = new AsyncRateLimiter(
  async (id: string) => await api.getData(id),
  {
    limit: 5,
    window: 1000,
    windowType: 'sliding',
    onError: (error) => {
      console.error('API call failed:', error);
    },
    onReject: (limiter) => {
      console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);
    }
  }
);

// Will execute immediately until limit reached, then block
// Returns the API response directly
const data = await rateLimiter.maybeExecute('123');

Type Parameters

• TFn extends AnyAsyncFunction

Constructors

new AsyncRateLimiter()

ts
new AsyncRateLimiter<TFn>(fn, initialOptions): AsyncRateLimiter<TFn>
new AsyncRateLimiter<TFn>(fn, initialOptions): AsyncRateLimiter<TFn>

Defined in: async-rate-limiter.ts:137

Parameters

fn

TFn

initialOptions

AsyncRateLimiterOptions<TFn>

Returns

AsyncRateLimiter<TFn>

Methods

getEnabled()

ts
getEnabled(): boolean
getEnabled(): boolean

Defined in: async-rate-limiter.ts:166

Returns the current enabled state of the rate limiter

Returns

boolean


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-rate-limiter.ts:325

Returns the number of times the function has errored

Returns

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

Defined in: async-rate-limiter.ts:339

Returns whether the function is currently executing

Returns

boolean


getLimit()

ts
getLimit(): number
getLimit(): number

Defined in: async-rate-limiter.ts:173

Returns the current limit of executions allowed within the time window

Returns

number


getMsUntilNextWindow()

ts
getMsUntilNextWindow(): number
getMsUntilNextWindow(): number

Defined in: async-rate-limiter.ts:300

Returns the number of milliseconds until the next execution will be possible For fixed windows, this is the time until the current window resets For sliding windows, this is the time until the oldest execution expires

Returns

number


getOptions()

ts
getOptions(): AsyncRateLimiterOptions<TFn>
getOptions(): AsyncRateLimiterOptions<TFn>

Defined in: async-rate-limiter.ts:159

Returns the current rate limiter options

Returns

AsyncRateLimiterOptions<TFn>


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: async-rate-limiter.ts:332

Returns the number of times the function has been rejected

Returns

number


getRemainingInWindow()

ts
getRemainingInWindow(): number
getRemainingInWindow(): number

Defined in: async-rate-limiter.ts:290

Returns the number of remaining executions allowed in the current window

Returns

number


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

Defined in: async-rate-limiter.ts:318

Returns the number of times the function has been settled

Returns

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

Defined in: async-rate-limiter.ts:311

Returns the number of times the function has been executed

Returns

number


getWindow()

ts
getWindow(): number
getWindow(): number

Defined in: async-rate-limiter.ts:180

Returns the current time window in milliseconds

Returns

number


maybeExecute()

ts
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>

Defined in: async-rate-limiter.ts:213

Attempts to execute the rate-limited function if within the configured limits. Will reject execution if the number of calls in the current window exceeds the limit. If execution is allowed, waits for any previous execution to complete before proceeding.

Error Handling:

  • If the rate-limited function throws and no onError handler is configured, the error will be thrown from this method.
  • If an onError handler is configured, errors will be caught and passed to the handler, and this method will return undefined.
  • If the rate limit is exceeded, the execution will be rejected and the onReject handler will be called if configured.
  • The error state can be checked using getErrorCount() and getIsExecuting().
  • Rate limit rejections can be tracked using getRejectionCount().

Parameters

args

...Parameters<TFn>

Returns

Promise<undefined | ReturnType<TFn>>

A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError

Throws

The error from the rate-limited function if no onError handler is configured

Example

ts
const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });

// First 5 calls will execute
await rateLimiter.maybeExecute('arg1', 'arg2');

// Additional calls within the window will be rejected
await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });

// First 5 calls will execute
await rateLimiter.maybeExecute('arg1', 'arg2');

// Additional calls within the window will be rejected
await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected

reset()

ts
reset(): void
reset(): void

Defined in: async-rate-limiter.ts:346

Resets the rate limiter state

Returns

void


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: async-rate-limiter.ts:152

Updates the rate limiter options Returns the new options state

Parameters

newOptions

Partial<AsyncRateLimiterOptions<TFn>>

Returns

void

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.