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

AsyncDebouncer

类:AsyncDebouncer<TFn>

定义于:async-debouncer.ts:101

一个创建异步防抖函数的类。

防抖确保函数仅在其最后一次调用后经过指定的延迟后才执行。 每次新的调用都会重置延迟计时器。这对于处理频繁事件(如窗口大小调整)非常有用 或输入更改,您只想在事件停止发生后执行处理程序。

与节流(允许定期执行)不同,防抖会阻止任何执行,直到 函数在指定的延迟期内停止被调用。

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

错误处理:

  • 如果在执行期间发生错误并且未提供 onError 处理程序,则该错误将被抛出并向上传播给调用者。
  • 如果提供了 onError 处理程序,错误将被捕获并传递给处理程序,而不是被抛出。
  • 可以使用 getErrorCount() 跟踪错误计数。
  • 防抖器会维护其状态,并且在发生错误后可以继续使用。

示例

ts
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
  const results = await searchAPI(value);
  return results; // 返回值被保留
}, {
  wait: 500,
  onError: (error) => {
    console.error('搜索失败:', error);
  }
});

// 在每次按键时调用,但仅在停止输入 500 毫秒后执行
// 直接返回 API 响应
const results = await asyncDebouncer.maybeExecute(inputElement.value);
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
  const results = await searchAPI(value);
  return results; // 返回值被保留
}, {
  wait: 500,
  onError: (error) => {
    console.error('搜索失败:', error);
  }
});

// 在每次按键时调用,但仅在停止输入 500 毫秒后执行
// 直接返回 API 响应
const results = await asyncDebouncer.maybeExecute(inputElement.value);

类型参数

TFn extends AnyAsyncFunction

构造函数

new AsyncDebouncer()

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

定义于:async-debouncer.ts:117

参数

fn

TFn

initialOptions

AsyncDebouncerOptions<TFn>

返回

AsyncDebouncer<TFn>

方法

cancel()

ts
cancel(): void
cancel(): void

定义于:async-debouncer.ts:259

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

返回

void


getEnabled()

ts
getEnabled(): boolean
getEnabled(): boolean

定义于:async-debouncer.ts:150

返回当前防抖器的启用状态

返回

boolean


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

定义于:async-debouncer.ts:288

返回函数出错的次数

返回

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

定义于:async-debouncer.ts:302

如果当前有执行正在进行,则返回 true

返回

boolean


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

定义于:async-debouncer.ts:295

如果存在排队等待尾随执行的待处理执行,则返回 true

返回

boolean


getLastResult()

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

定义于:async-debouncer.ts:267

返回防抖函数的最后结果

返回

undefined | ReturnType<TFn>


getOptions()

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

定义于:async-debouncer.ts:143

返回当前防抖器选项

返回

AsyncDebouncerOptions<TFn>


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

定义于:async-debouncer.ts:281

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

返回

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

定义于:async-debouncer.ts:274

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

返回

number


getWait()

ts
getWait(): number
getWait(): number

定义于:async-debouncer.ts:157

返回当前防抖器的等待状态

返回

number


maybeExecute()

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

定义于:async-debouncer.ts:175

尝试执行防抖函数。 如果已有调用正在进行,则会将其排队。

错误处理:

  • 如果防抖函数抛出错误并且未配置 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-debouncer.ts:131

更新防抖器选项

参数

newOptions

Partial<AsyncDebouncerOptions<TFn>>

返回

void