2. class Scheduler { constructor(parallelCount = 2) { this.parallelCount = parallelCount; this.runningCount = 0; this.task = []; } add(task) { return new Promise((resolve, reject) => { this.task.push({ task, resolve, reject, }); this._run(); }); } _run() { if (this.task.length > 0 && this.runningCount < this.parallelCount) { const { task, resolve, reject } = this.task.shift(); this.runningCount++; task() .then(resolve, reject) .finally(() => { this.runningCount--; this._run(); }); } } }
点赞 评论

相关推荐

劝退式:感觉有人回才是不正常的
点赞 评论 收藏
分享
Rena1ssance_:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
牛客网
牛客企业服务