码上!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const maxNum = 3
const allfilesList = [假设有一百条数据]
const aloneProcessingFn = (file) =>{
return new Promise((resolve, reject) =>{
})
}
const controlConcurrencyFn = (maxNum, allfilesList, aloneProcessingFn) => {
const allAsyncTask = [];
const executingTask = [];
for(const item of allfilesList) {
const pr = Promise.reslove().then(() => aloneProcessingFn(item))
allAsyncTask.push(pr)
if(maxNum <= allfilesList.length){
const executePr = pr.then(() =>
executingTask.splice(executingTask.indexOf(executePr), 1))
).catch(() =>
executingTask.splice(executingTask.indexOf(executePr), 1)
)
executingTask.push(e)
if (executingTask.length >= maxNum) {
await Promise.race(executingTask)
}
}
}
return Promise.allSettled(allAsyncTask);
}
controlConcurrencyFn(maxNum, allfilesList, aloneProcessingFn).then(() =>{
}).catch(() =>{
})