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

AsyncRateLimiter

Class: AsyncRateLimiter<TFn, TArgs>

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

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.

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.

Example

ts
const rateLimiter = new AsyncRateLimiter(
  async (id: string) => await api.getData(id),
  { limit: 5, window: 1000 } // 5 calls per second
);

// Will execute immediately until limit reached, then block
await rateLimiter.maybeExecute('123');
const rateLimiter = new AsyncRateLimiter(
  async (id: string) => await api.getData(id),
  { limit: 5, window: 1000 } // 5 calls per second
);

// Will execute immediately until limit reached, then block
await rateLimiter.maybeExecute('123');

Type Parameters

TFn extends AnyAsyncFunction

TArgs extends Parameters<TFn>

Constructors

new AsyncRateLimiter()

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

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

Parameters

fn

TFn

initialOptions

AsyncRateLimiterOptions<TFn, TArgs>

Returns

AsyncRateLimiter<TFn, TArgs>

Methods

getExecutionCount()

ts
getExecutionCount(): number
getExecutionCount(): number

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

Returns the number of times the function has been executed

Returns

number


getMsUntilNextWindow()

ts
getMsUntilNextWindow(): number
getMsUntilNextWindow(): number

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

Returns the number of milliseconds until the next execution will be possible

Returns

number


getOptions()

ts
getOptions(): Required<AsyncRateLimiterOptions<TFn, TArgs>>
getOptions(): Required<AsyncRateLimiterOptions<TFn, TArgs>>

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

Returns the current rate limiter options

Returns

Required<AsyncRateLimiterOptions<TFn, TArgs>>


getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

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

Returns the number of times the function has been rejected

Returns

number


getRemainingInWindow()

ts
getRemainingInWindow(): number
getRemainingInWindow(): number

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

Returns the number of remaining executions allowed in the current window

Returns

number


maybeExecute()

ts
maybeExecute(...args): Promise<boolean>
maybeExecute(...args): Promise<boolean>

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

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.

Parameters

args

...TArgs

Returns

Promise<boolean>

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:208

Resets the rate limiter state

Returns

void


setOptions()

ts
setOptions(newOptions): AsyncRateLimiterOptions<TFn, TArgs>
setOptions(newOptions): AsyncRateLimiterOptions<TFn, TArgs>

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

Updates the rate limiter options Returns the new options state

Parameters

newOptions

Partial<AsyncRateLimiterOptions<TFn, TArgs>>

Returns

AsyncRateLimiterOptions<TFn, TArgs>

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.