Skip to content

Commit

Permalink
interview
Browse files Browse the repository at this point in the history
  • Loading branch information
okbug committed Feb 17, 2022
1 parent 764fa5d commit 5ed62b1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions interviews/22-02-17-feishu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 手写题目

JS 实现一个带并发限制的异步调度器 Scheduler

题目描述
// JS 实现一个带并发限制的异步调度器 Scheduler
// 保证同时运行的任务最多有两个

class Scheduler {
constructor() {
this._max = 2;
}

}

const timeout = (time) => new Promise(resolve => {
setTimeout(resolve, time)
})


const scheduler = new Scheduler()
const addTask = (time, order) => {
scheduler
.add(() => timeout(time))
.then(() => console.log(order))
}


addTask(2000, '1');
addTask(1000, '2');
addTask(3000, '3');
addTask(2000, '4');

2 第一秒
1 第二秒
3 第四秒
4 第四秒

0 comments on commit 5ed62b1

Please sign in to comment.