promise在异步操作中经常遇到,对多个并发异步过程的处理Promise自身有Promise.all() Promise.allSettled() Promise.race()等,但都没有对并发数量进行控制。
本文实现了一个Schedule类,可以用来对并发数进行控制。
废话不说,上才艺~
class Schedule{
constructor(maxNum){
this.list = [];
this.maxNum = maxNum
this.workingNum = 0
}
add(promiseCreator){
this.list.push(promiseCreator)
}
start(){
for (let index = 0; index < this.maxNum; index++) {
this.doNext()
}
}
doNext(){
if(this.list.length && this.workingNum < this.maxNum){
this.workingNum++;
const promise = this.list.shift();
promise().then(()=>{
this.workingNum--;
this.doNext();
})
}
}
}
const timeout = time => new Promise((resolve)=>{
setTimeout(resolve, time)
})
const schedule = new Schedule(2);
const addTask = (time, order)=>{
schedule.add(()=>timeout(time).then(()=>{
console.log(order);
}))
}
addTask(1000, 1)
addTask(500, 2)
addTask(300, 3)
addTask(400, 4)
schedule.start()
复制代码
执行结果
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END