-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromises.js
56 lines (50 loc) · 1.16 KB
/
Promises.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
// race
// all
// any
// allsettled
const p1 = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
reject('p1');
}, 50);
});
const login = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('a');
}, 100);
});
const users = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('b');
}, 200);
});
const apiCalls = async () => {
try {
// console.time('noraml promise')
// const r1 = await login();
// console.log(r1);
// const r2 = await users(r1);
// console.timeEnd('noraml promise')
// console.log(r2);
console.time('promise all');
const result = await Promise.allSettled([p1(), login(), users()]);
console.log(result);
console.log(result.filter(x => x.status === 'fulfilled'));
console.timeEnd('promise all');
} catch (error) {
console.log(error);
} finally {
console.log('finally call always');
}
};
apiCalls();
// old javascript technic
// p1()
// .then((value) =>
// p2(value)
// .then((val) => console.log(val))
// .catch((err) => console.log(err))
// )
// .catch((err) => console.log("err", err));