-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promise-Limit.html
52 lines (51 loc) · 1.49 KB
/
Promise-Limit.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script>
// 递归版
const promiseLimitByDepth = (promises, limit) => {
const { length } = promises
limit = limit > length ? length : limit
return new Promise(resolve => {
let finishedNum = 0, i = 0, res = []
const request = async () => {
res[i] = await promises[i]
finishedNum++
console.log(finishedNum, i, limit)
if (i < length) {
request()
i++
}
if (finishedNum >= length) {
resolve(res)
}
}
for (; i < limit; i++) {
request()
}
})
}
const newFetch = (delay) => {
return new Promise((resolve) => {
setTimeout(() => {
console.log(delay, 'done.')
resolve(delay);
}, delay);
});
};
// promiseLimitByDepth([2000, 1000, 3000, 2500, 1200, 5000, 3500, 2300].map(delay => newFetch(delay)), 2).then(console.log);
const createPromiseLimit = limit => {
return async promises => await promiseLimitByDepth(promises, limit)
}
const promiseLimitByDepth2 = createPromiseLimit(2)
promiseLimitByDepth2([2000, 1000, 3000, 2500, 1200, 5000, 3500, 2300].map(delay => newFetch(delay)))
</script>
</body>
</html>