40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
exports.processInBatches = async (arr, handler, batchSize = 10) => {
|
|
const result = [];
|
|
for (let i = 0; i < arr.length; i += batchSize) {
|
|
const batch = arr.slice(i, i + batchSize);
|
|
const batchResult = await Promise.all(batch.map(handler));
|
|
result.push(...batchResult);
|
|
}
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 批量执行任务,控制最大并发数
|
|
* @param {Array} items - 待处理的任务参数列表
|
|
* @param {Function} taskFn - 单个任务的处理函数,需返回 Promise
|
|
* @param {number} maxConcurrency - 最大并发数
|
|
* @returns {Promise<Array>} - 返回所有任务的结果
|
|
*/
|
|
exports.executeWithConcurrency = async (items, taskFn, maxConcurrency = 10) => {
|
|
let index = 0;
|
|
const results = [];
|
|
|
|
while (index < items.length) {
|
|
// 获取当前批次
|
|
const batch = items.slice(index, index + maxConcurrency);
|
|
|
|
// 映射当前批次的任务并执行
|
|
const promises = batch.map((item) => taskFn(item));
|
|
// 等待当前批次的任务完成
|
|
const batchResults = await Promise.all(promises);
|
|
|
|
// 收集结果
|
|
results.push(...batchResults);
|
|
|
|
// 更新索引
|
|
index += maxConcurrency;
|
|
}
|
|
|
|
return results;
|
|
};
|