-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcurrentRequest.js
60 lines (54 loc) · 1.18 KB
/
concurrentRequest.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
/**
* 对异步请求并发限制
* @param {any[]} list 待处理数组
* @param {function} fn 处理操作
* @param {number} limit 并发限制数量
* @returns 处理结果
*/
function concurrentRequest(list, fn, limit) {
return new Promise(resolve => {
let curIdx = 0;
let resolvedCnt = 0;
const result = [];
const len = list.length;
function next() {
const index = curIdx++;
Promise.resolve(list[index])
.then(v => fn(v))
.then(v => {
result[index] = v;
resolvedCnt++;
if (resolvedCnt === len) {
resolve(result);
}
if (curIdx < len) next();
});
}
for (let i = 0; i < len && i < limit; i++) {
next();
}
});
}
// test
const delay = (fn, seconds, ...args) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
Promise.resolve(fn(...args))
.then(resolve)
.catch(reject);
}, seconds);
});
};
concurrentRequest(
[1, 1, 1, 1, 1],
x => delay(() => console.log(new Date().getTime()), 1000),
2
);
// 注意输出时间,两个一组,每组隔大约 1000 ms
/*
1670061375602
1670061375603
1670061376619
1670061376620
1670061377634
*/