简单想了下,并发那个 这样可行不 class Scheduler { constructor() { this.count = 2; //并发数量 this.arr = []; //事件 } //不能直接new Promise addTask(time, str) { const fn = (resolve) => { setTimeout(() => { console.log(str); resolve(); }, time * 1000); }; this.arr.push(fn); } start() { let p = this.count; for (let i = 0; i < p; i++) { if (this.arr.length <= 0) return; let fn = this.arr.shift(); new Promise((resolve) => { this.count--; fn(resolve); }).then(() => { this.count++; this.start(); }); } } }
点赞 4

相关推荐

2024-12-20 15:55
门头沟学院 数据运营
点赞 评论 收藏
分享
牛客网
牛客企业服务