function asyncRateLimit<TFn>(fn, initialOptions): (...args) => Promise<ReturnType<TFn> | undefined>; function asyncRateLimit<TFn>(fn, initialOptions): (...args) => Promise<ReturnType<TFn> | undefined>; Defined in: async-rate-limiter.ts:637
Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.
Async vs Sync Versions: The async version provides advanced features over the sync rate limit function:
The sync rate limit function is lighter weight and simpler when you don't need async features, return values, or execution control.
What is Rate Limiting? Rate limiting 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.
Window Types:
Configuration Options:
When to Use Rate Limiting: 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:
State Management:
TFn extends AnyAsyncFunction
TFn
(...args): Promise<ReturnType<TFn> | undefined>; (...args): Promise<ReturnType<TFn> | undefined>; 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.
Error Handling:
...Parameters<TFn>
Promise<ReturnType<TFn> | undefined>
A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
The error from the rate-limited function if no onError handler is configured
const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 }); // First 5 calls will return a promise that resolves with the result const result = await rateLimiter.maybeExecute('arg1', 'arg2'); // Additional calls within the window will return undefined const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 }); // First 5 calls will return a promise that resolves with the result const result = await rateLimiter.maybeExecute('arg1', 'arg2'); // Additional calls within the window will return undefined const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined // Rate limit to 5 calls per minute with a sliding window const rateLimited = asyncRateLimit(makeApiCall, { limit: 5, window: 60000, windowType: 'sliding', onError: (error) => { console.error('API call failed:', error); }, onReject: (rateLimiter) => { console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`); } }); // First 5 calls will execute immediately // Additional calls will be rejected until the minute window resets // Returns the API response directly const result = await rateLimited(); // For more even execution, consider using throttle instead: const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds // Rate limit to 5 calls per minute with a sliding window const rateLimited = asyncRateLimit(makeApiCall, { limit: 5, window: 60000, windowType: 'sliding', onError: (error) => { console.error('API call failed:', error); }, onReject: (rateLimiter) => { console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`); } }); // First 5 calls will execute immediately // Additional calls will be rejected until the minute window resets // Returns the API response directly const result = await rateLimited(); // For more even execution, consider using throttle instead: const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds 