Framework
Version
防抖器 API 參考
節流器 API 參考
速率限制器 API 參考
佇列 API 參考

asyncThrottle

Function: asyncThrottle()

ts
function asyncThrottle<TFn, TArgs>(fn, initialOptions): (...args) => Promise<void>
function asyncThrottle<TFn, TArgs>(fn, initialOptions): (...args) => Promise<void>

Defined in: async-throttler.ts:223

Creates an async throttled function that limits how often the function can execute. The throttled function will execute at most once per wait period, even if called multiple times. If called while executing, it will wait until execution completes before scheduling the next call.

Type Parameters

TFn extends AnyAsyncFunction

TArgs extends any[]

Parameters

fn

TFn

initialOptions

Omit<AsyncThrottlerOptions<TFn, TArgs>, "enabled">

Returns

Function

Attempts to execute the throttled function If a call is already in progress, it may be blocked or queued depending on the wait option

Parameters

args

...TArgs

Returns

Promise<void>

Example

ts
const throttled = asyncThrottle(async () => {
  await someAsyncOperation();
}, { wait: 1000 });

// This will execute at most once per second
await throttled();
await throttled(); // Waits 1 second before executing
const throttled = asyncThrottle(async () => {
  await someAsyncOperation();
}, { wait: 1000 });

// This will execute at most once per second
await throttled();
await throttled(); // Waits 1 second before executing
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.