框架
版本
防抖器 API 参考
节流器 API 参考
速率限制器 API 参考
队列 API 参考
批处理器 API 参考

异步节流器

Class: AsyncThrottler<TFn>

定义于: async-throttler.ts:199

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

节流限制函数执行的频率,在指定的时段内只允许执行一次。与去抖动(debouncing)不同,去抖动会在每次调用时重置延迟计时器,而节流则确保函数以固定的间隔执行,无论调用频率如何。

与非异步的Throttler不同,这个异步版本支持从节流函数返回,这使其成为API调用和其他异步操作的理想选择,当您希望从 maybeExecute 调用中获取结果,而不是在节流函数内部将结果设置到状态变量时。

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

错误处理

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

状态管理

  • 使用 TanStack Store 进行响应式状态管理
  • 使用 initialState 来在创建异步节流器时提供初始状态值
  • 使用 onSuccess 回调函数来响应成功的函数执行并实现自定义逻辑
  • 使用 onError 回调函数来响应函数执行错误并实现自定义错误处理
  • 使用 onSettled 回调函数来响应函数执行完成(成功或失败)并实现自定义逻辑
  • 状态包括错误计数、执行状态、上次执行时间以及成功/完成计数
  • 当直接使用类时,状态可以通过 asyncThrottler.store.state 访问
  • 使用框架适配器(React/Solid)时,状态从 asyncThrottler.state 访问

示例

ts
const throttler = new AsyncThrottler(async (value: string) => {
  const result = await saveToAPI(value);
  return result; // Return value is preserved
}, {
  wait: 1000,
  onError: (error) => {
    console.error('API call failed:', error);
  }
});

// Will only execute once per second no matter how often called
// Returns the API response directly
const result = await throttler.maybeExecute(inputElement.value);
const throttler = new AsyncThrottler(async (value: string) => {
  const result = await saveToAPI(value);
  return result; // Return value is preserved
}, {
  wait: 1000,
  onError: (error) => {
    console.error('API call failed:', error);
  }
});

// Will only execute once per second no matter how often called
// Returns the API response directly
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:211

参数

fn

TFn

initialOptions

AsyncThrottlerOptions<TFn>

Returns (返回)

AsyncThrottler<TFn>

属性

fn

ts
fn: TFn;
fn: TFn;

定义于: async-throttler.ts:212


key

ts
key: string;
key: string;

定义于: async-throttler.ts:203


options

ts
options: AsyncThrottlerOptions<TFn>;
options: AsyncThrottlerOptions<TFn>;

定义于: async-throttler.ts:204


store

ts
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>;
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>;

定义于: async-throttler.ts:200

方法

cancel()

ts
cancel(): void
cancel(): void

定义于: async-throttler.ts:444

取消任何挂起的执行或中止正在进行的执行

Returns (返回)

void


flush()

ts
flush(): Promise<undefined | ReturnType<TFn>>
flush(): Promise<undefined | ReturnType<TFn>>

定义于: async-throttler.ts:393

立即处理当前挂起的执行

Returns (返回)

Promise<undefined | ReturnType<TFn>>


maybeExecute()

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

定义于: async-throttler.ts:300

尝试执行节流函数。执行行为取决于节流器选项

  • 如果自上次执行以来已经过去了足够的时间(>=等待周期)

    • 当 leading=true 时:立即执行
    • 当 leading=false 时:等待下一次尾部执行
  • 在等待周期内

    • 当 trailing=true 时:安排在等待周期结束时执行
    • 当 trailing=false 时:丢弃执行

参数

args

...Parameters<TFn>

Returns (返回)

Promise<undefined | ReturnType<TFn>>

示例

ts
const throttled = new AsyncThrottler(fn, { wait: 1000 });

// First call executes immediately
await throttled.maybeExecute('a', 'b');

// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');
const throttled = new AsyncThrottler(fn, { wait: 1000 });

// First call executes immediately
await throttled.maybeExecute('a', 'b');

// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');

reset()

ts
reset(): void
reset(): void

定义于: async-throttler.ts:452

将去抖器状态重置为其默认值

Returns (返回)

void


setOptions()

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

定义于: async-throttler.ts:232

更新异步节流器选项

参数

newOptions

Partial<AsyncThrottlerOptions<TFn>>

Returns (返回)

void

我们的合作伙伴
Code Rabbit
Unkey
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。