class Scheduler { constructor() { this.count = 0 this.waitQueue = []; } add(promiseCreator) { if (this.count < 2) { this.count += 1; return this.run(promiseCreator) } else { return new Promise(resolve => { this.waitQueue.push(() => promiseCreator().then(resolve)); }) } } run(promiseCreator) { return promiseCreator().then(() => { this.count -= 1; if (this.waitQueue.length) { this.run(this.waitQueue.shift()) } }); } }
点赞 1

相关推荐

牛客网
牛客企业服务