常宁做网站,西安做网站需要多少钱,哈尔滨公司网站开发,怎么发布信息到百度思路#xff1a;
主要通过异步等待队列执行的原理。
当前执行的任务数达到最大值的时候#xff0c;再继续执行的任务会放入等待队列里#xff0c;直到当前任务执行结束后#xff0c;减少一个当前任务数#xff0c;并且判断队列中是否有任务#xff0c;如果有则按顺序执… 思路
主要通过异步等待队列执行的原理。
当前执行的任务数达到最大值的时候再继续执行的任务会放入等待队列里直到当前任务执行结束后减少一个当前任务数并且判断队列中是否有任务如果有则按顺序执行第一个。
class Scheduler {constructor(max) {this.max max;this.count 0; // 用来记录当前正在执行的异步函数this.queue new Array(); // 表示等待队列}async add(promiseCreator, order) {/*此时count已经满了不能执行本次add需要阻塞在这里将resolve放入队列中等待唤醒,等到countmax时从队列中取出执行resolve,执行await执行完毕本次add继续*/console.log(添加事件, order);if (this.count this.max) {console.log(放入队列中, order);await new Promise((resolve, reject) this.queue.push(resolve));}this.count;console.log(任务执行, order);let res await promiseCreator();this.count--;if (this.queue.length) {console.log(队列启动);// 依次唤醒add// 若队列中有值将其resolve弹出并执行// 以便阻塞的任务可以正常执行this.queue.shift()();}return res;}
}const timeout time new Promise(resolve {setTimeout(resolve, time);});const scheduler new Scheduler(2);const addTask (time, order) {//add返回一个promise参数也是一个promisescheduler.add(() timeout(time), order).then(() console.log(order));
};addTask(1000, 1);
addTask(500, 2);
addTask(300, 3);
addTask(400, 4);// output: 2 3 1 4