框架
版本
Debouncer API 参考
Throttler API 参考
速率限制器 API 参考
队列 API 参考
批处理器 API 参考
批处理器示例

AsyncThrottler

类:AsyncThrottler<TFn>

定义于:async-throttler.ts:105

一个创建异步节流函数的类。

节流限制函数的执行频率,在指定的时间窗口内只允许一次执行。 与每次调用都会重置延迟计时器的防抖不同,节流确保函数以固定的时间间隔执行,无论调用频率如何。

与非异步 Throttler 不同,此异步版本支持从节流函数返回值, 使其非常适合 API 调用和其他异步操作,在这些操作中,您希望获得 maybeExecute 调用的结果 而不是在节流函数内部设置状态变量的结果。

这对于限制 API 调用速率、处理滚动/调整大小事件或任何需要确保最大执行频率的场景非常有用。

错误处理:

  • 如果提供了 onError 处理程序,它将与错误和节流器实例一起被调用
  • 如果 throwOnError 为 true(未提供 onError 处理程序时的默认值),则会抛出错误
  • 如果 throwOnError 为 false(提供 onError 处理程序时的默认值),则会吞没错误
  • onError 和 throwOnError 可以一起使用——处理程序将在抛出任何错误之前被调用
  • 可以使用底层的 AsyncThrottler 实例检查错误状态

示例

ts
const throttler = new AsyncThrottler(async (value: string) => {
  const result = await saveToAPI(value);
  return result; // 返回值被保留
}, {
  wait: 1000,
  onError: (error) => {
    console.error('API 调用失败:', error);
  }
});

// 无论调用频率如何,每秒仅执行一次
// 直接返回 API 响应
const result = await throttler.maybeExecute(inputElement.value);
const throttler = new AsyncThrottler(async (value: string) => {
  const result = await saveToAPI(value);
  return result; // 返回值被保留
}, {
  wait: 1000,
  onError: (error) => {
    console.error('API 调用失败:', error);
  }
});

// 无论调用频率如何,每秒仅执行一次
// 直接返回 API 响应
const result = await throttler.maybeExecute(inputElement.value);

类型参数

TFn extends AnyAsyncFunction

构造函数

new AsyncThrottler()

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

定义于:async-throttler.ts:121

参数

fn

TFn

initialOptions

AsyncThrottlerOptions<TFn>

返回

AsyncThrottler<TFn>

方法

cancel()

ts
cancel(): void
cancel(): void

定义于:async-throttler.ts:260

取消任何待处理的执行或中止任何正在进行的执行

返回

void


getEnabled()

ts
getEnabled(): boolean
getEnabled(): boolean

定义于:async-throttler.ts:154

返回节流器的当前启用状态

返回

boolean


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

定义于:async-throttler.ts:311

返回函数出错的次数

返回

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

定义于:async-throttler.ts:325

返回当前执行状态

返回

boolean


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

定义于:async-throttler.ts:318

返回当前待处理状态

返回

boolean


getLastExecutionTime()

ts
getLastExecutionTime(): number
getLastExecutionTime(): number

定义于:async-throttler.ts:276

返回上次执行时间

返回

number


getLastResult()

ts
getLastResult(): undefined | ReturnType<TFn>
getLastResult(): undefined | ReturnType<TFn>

定义于:async-throttler.ts:290

返回防抖函数的最后结果

返回

undefined | ReturnType<TFn>


getNextExecutionTime()

ts
getNextExecutionTime(): number
getNextExecutionTime(): number

定义于:async-throttler.ts:283

返回下次执行时间

返回

number


getOptions()

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

定义于:async-throttler.ts:147

返回当前选项

返回

AsyncThrottlerOptions<TFn>


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

定义于:async-throttler.ts:304

返回函数已完成(完成或出错)的次数

返回

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

定义于:async-throttler.ts:297

返回函数已成功执行的次数

返回

number


getWait()

ts
getWait(): number
getWait(): number

定义于:async-throttler.ts:161

返回当前等待时间(以毫秒为单位)

返回

number


maybeExecute()

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

定义于:async-throttler.ts:179

尝试执行节流函数。 如果已有调用正在进行,则根据 wait 选项,它可能会被阻塞或排队。

错误处理:

  • 如果节流函数抛出错误并且未配置 onError 处理程序, 则此方法将抛出错误。
  • 如果配置了 onError 处理程序,错误将被捕获并传递给处理程序, 并且此方法将返回 undefined。
  • 可以使用 getErrorCount()getIsExecuting() 检查错误状态。

参数

args

...Parameters<TFn>

返回

Promise<undefined | ReturnType<TFn>>

一个 Promise,它解析为函数的返回值,如果发生错误并由 onError 处理,则解析为 undefined

抛出

如果未配置 onError 处理程序,则抛出节流函数的错误


setOptions()

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

定义于:async-throttler.ts:135

更新节流器选项

参数

newOptions

Partial<AsyncThrottlerOptions<TFn>>

返回

void