1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| class BatchProcessor {
constructor(batchSize = 10) {
this.batchSize = batchSize;
this.queue = [];
}
async add(task) {
this.queue.push(task);
if (this.queue.length >= this.batchSize) {
await this.processBatch();
}
}
async processBatch() {
const batch = this.queue.splice(0, this.batchSize);
// 并行处理
const results = await Promise.all(
batch.map(task => this.process(task))
);
return results;
}
}
|