-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut9.jsx
80 lines (51 loc) · 1.68 KB
/
tut9.jsx
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// promise api in js
let p1=new Promise((resolve , reject) =>{
setTimeout(() => {
resolve(12);
// reject(121);
}, 1000);
})
let p2=new Promise((resolve , reject) =>{
setTimeout(() => {
resolve(13);
}, 2000);
})
let p3=new Promise((resolve , reject) =>{
setTimeout(() => {
resolve(14);
}, 3000);
});
// p1.then((result) => {
// console.log("i am promise 1 and i resolved with a value of " +result);
// })
// p2.then((result) => {
// console.log("i am promise 2 and i resolved with a value of " +result);
// })
// p3.then((result) => {
// console.log("i am promise 1 and i resolved with a value of " +result);
// })
// now we have to resolve all the three promise at once
// let promise_all=Promise.all([p1,p2,p3]);
// promise_all.then((value)=>{
// console.log(value);
// }).catch((value)=>{
// console.log(value);
// })
// it will print the array of the resolve values but if in any case any value isnt resolve and get rejected then this process wont work
// so to avoid this error we use another method
// let promise_allSet=Promise.allSettled([p1,p2,p3]);
// promise_allSet.then((value)=>{
// console.log(value);
// });
// you will know the status and value of your promise
// 3rd method promise.racce
let promise_race=Promise.race([p1,p2,p3]);
promise_race.then((value)=>{
console.log(value);
})
// it will print that promise which resolve first but this will print the value if the promise which resolved first is rejected
// for that we have another method
let promise_any=Promise.any([p1,p2,p3]);
promise_any.then((value)=>{
console.log((value));
})