function asyncBatch<TValue>(fn, options): (item) => void
function asyncBatch<TValue>(fn, options): (item) => void
定义于: async-batcher.ts:460
创建一个异步批处理程序,它将项目分批处理
与同步批处理程序不同,此异步版本
错误处理
状态管理
• TValue
(items) => Promise<any>
AsyncBatcherOptions<TValue>
Function
将一项添加到异步批处理程序中。如果达到批次大小、发生超时或 shouldProcess 返回 true,则将处理该批次
TValue
void
const batchItems = asyncBatch<number>(
async (items) => {
const result = await processApiCall(items);
console.log('Processing:', items);
return result;
},
{
maxSize: 3,
wait: 1000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batchItems(1);
batchItems(2);
batchItems(3); // Triggers batch processing
const batchItems = asyncBatch<number>(
async (items) => {
const result = await processApiCall(items);
console.log('Processing:', items);
return result;
},
{
maxSize: 3,
wait: 1000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batchItems(1);
batchItems(2);
batchItems(3); // Triggers batch processing
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。