-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise-all.js
53 lines (40 loc) · 942 Bytes
/
promise-all.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
const { getData, getDataFail, output } = require('./utils');
function bulkGetData() {
const total = 100;
let i = 1;
const promises = [];
while (i <= total) {
const pr = i % 10 === 0 ? getDataFail(i) : getData(i);
// const pr = getData(i);
promises.push(
pr
.then(output)
.catch(output)
);
i++;
}
return Promise.all(promises)
.then((res) => output(res, 'All done!'))
.catch(output);
}
async function bulkGetDataInChunk(total, chunk) {
let i = 1;
let promises = [];
while (i <= total) {
const pr = i % 10 === 0 ? getDataFail(i) : getData(i);
promises.push(
pr
.then(output)
.catch(output)
);
if (i % chunk === 0) {
await Promise.all(promises)
.then(() => output('complete chunk ' + i / chunk));
promises = [];
}
i++;
}
output('All done!');
}
bulkGetData();
// bulkGetDataInChunk(100, 10);