定义于: async-batcher.ts:230
一个异步收集和处理项目批次的类。
这是 Batcher 类的异步版本。与同步版本不同,这个异步批处理程序
批处理是一种将多个操作组合在一起作为单个单元进行处理的技术。
AsyncBatcher 提供了一种灵活的方式来实现具有可配置的异步批处理
错误处理
状态管理
const batcher = new AsyncBatcher<number>(
async (items) => {
const result = await processItems(items);
console.log('Processing batch:', items);
return result;
},
{
maxSize: 5,
wait: 2000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed and the result will be available
// batcher.execute() // manually trigger a batch
const batcher = new AsyncBatcher<number>(
async (items) => {
const result = await processItems(items);
console.log('Processing batch:', items);
return result;
},
{
maxSize: 5,
wait: 2000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed and the result will be available
// batcher.execute() // manually trigger a batch
• TValue
new AsyncBatcher<TValue>(fn, initialOptions): AsyncBatcher<TValue>
new AsyncBatcher<TValue>(fn, initialOptions): AsyncBatcher<TValue>
定义于: async-batcher.ts:238
(items) => Promise<any>
AsyncBatcherOptions<TValue>
AsyncBatcher<TValue>
fn: (items) => Promise<any>;
fn: (items) => Promise<any>;
定义于: async-batcher.ts:239
TValue[]
Promise<any>
key: string;
key: string;
定义于: async-batcher.ts:234
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
定义于: async-batcher.ts:235
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
定义于: async-batcher.ts:231
addItem(item): void
addItem(item): void
定义于: async-batcher.ts:297
向异步批处理程序添加一个项目。如果达到批次大小、超时或 shouldProcess 返回 true,则批次将被处理。
TValue
void
clear(): void
clear(): void
定义于: async-batcher.ts:398
从异步批处理程序中移除所有项目。
void
flush(): Promise<any>
flush(): Promise<any>
定义于: async-batcher.ts:372
立即处理当前批次的项目。
Promise<any>
peekAllItems(): TValue[]
peekAllItems(): TValue[]
定义于: async-batcher.ts:380
返回异步批处理程序中所有项目的副本。
TValue[]
peekFailedItems(): TValue[]
peekFailedItems(): TValue[]
定义于: async-batcher.ts:384
TValue[]
reset(): void
reset(): void
定义于: async-batcher.ts:405
将异步批处理程序的状态重置为其默认值。
void
setOptions(newOptions): void
setOptions(newOptions): void
定义于: async-batcher.ts:260
更新异步批处理程序的选项。
Partial<AsyncBatcherOptions<TValue>>
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。