-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
42 lines (38 loc) · 1.21 KB
/
tasks.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
class ConcurrentTaskQueue {
constructor(taskPromisesFunc = [], batchSize = 1) {
this.batchSize = batchSize > taskPromisesFunc.length ? taskPromisesFunc.length : batchSize
this.todoTasks = taskPromisesFunc
this.resolvedValues = []
}
run(resolve, reject) {
if (this.todoTasks.length > 0) {
const taskPromises = this.todoTasks.splice(0, this.batchSize);
Promise.all(taskPromises.map((p) => p()))
.then((resolvedValues) => {
this.resolvedValues = [...this.resolvedValues, ...resolvedValues]
this.run(resolve, reject)
})
.catch((err) => reject(err))
} else {
resolve(this.resolvedValues)
}
}
runTasks() {
return new Promise((resolve, reject) => {
this.run(resolve, reject)
})
}
}
module.exports.ConcurrentTaskQueue = ConcurrentTaskQueue;
// const batchSize = 2;
// const taskQueue = new ConcurrentTaskQueue([
// // wrap all functions to prevent direct execution
// () => costlyFunction(10),
// () => costlyFunction(20),
// () => costlyFunction(100),
// () => costlyFunction(50),
// ], batchSize);
// taskQueue.runTasks()
// .then(([res1, res2, res3, res4]) => {
// console.log(res1, res2, res3, res4);
// });