-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
87 lines (83 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Task Runner
*/
class TaskRunner {
constructor(allowedTaskCount) {
this.allowedTaskCount = allowedTaskCount;
this.currentTaskCount = 0;
this.waitList = [];
}
push(task) {
this.waitList.push(task);
this.runTask();
}
runTask() {
if(this.currentTaskCount < this.allowedTaskCount) {
if(this.waitList.length) {
const task = this.waitList.shift();
task().then(res => {
console.log(task.name, res);
this.currentTaskCount--;
this.runTask();
});
this.currentTaskCount++;
console.log(`Running - ${task.name}`);
}
}
}
}
// const runner = new TaskRunner(3);
// runner.push(task1);
// runner.push(task2);
// runner.push(task3);
// runner.push(task4);
// runner.push(task5);
// runner.push(task6);
//
// function task1() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 1 Completed');
// }, 100);
// })
// }
//
// function task2() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 2 Completed');
// }, 200);
// })
// }
//
// function task3() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 3 Completed');
// }, 300);
// })
// }
//
// function task4() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 4 Completed');
// }, 400);
// })
// }
//
// function task5() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 5 Completed');
// }, 500);
// })
// }
//
// function task6() {
// return new Promise((resolve) => {
// setTimeout(() => {
// resolve('Task 6 Completed');
// }, 600);
// })
// }