-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromiseTypes.js
More file actions
59 lines (43 loc) · 2.24 KB
/
PromiseTypes.js
File metadata and controls
59 lines (43 loc) · 2.24 KB
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
/* here we have 4tyesp
1) Promise.all();
2) Promise.allsettled();
3) Promise.race();
4)Promise.any();*/
const p1=new Promise(function(resolve,reject){
setTimeout(()=>resolve("P1 Success"),3000);
});
const p2=new Promise(function(resolve,reject){
setTimeout(()=>reject("P2 fail"),1000); //as we should not contain uncaught error so that should be caught them before broswer caughts it by using catch
// setTimeout(resolve("P2 Success"),1000);
})
const p3=new Promise(function(resolve,reject){
setTimeout(()=>reject("P3 Success"),2000);
})
// console.log(Promise.all([p1,p2,p3]));//first fail
// in success case it waits fro all promices to be settled and then return all values whereas in failure case as soon as it encounters failure it directly return the same error for all of them
Promise.all([p1,p2,p3]).then((value)=>{
console.log("Promise.all",value)
}).catch((err)=>console.log(err)
)
//its some what similar to promise.all in success case although in failure case it behavious same like success whether it failure or success it waits for all to be settled and then return output of all them waht ever it settled
Promise.allSettled([p1,p2,p3]).then((value)=>{
console.log("Promise.allSettled",value)
}).catch((err)=>console.log(err)
)
//its name suggest that it returns as soon as any of the promicess settled either resolved or rejected does not wait for others whether their can be resolved or rejected does not matter
Promise.race([p1,p2,p3]).then((value)=>{
console.log("Promise.race",value)
}).catch((err)=>console.log(err)
)
//its name suggest that it returns as soon as any of the promicess settled either resolved or rejected does not wait for others whether their can be resolved or rejected does not matter
Promise.any([p1,p2,p3]).then((value)=>{
console.log("Promise.any",value)
}).catch((err)=>console.log(err)
)
// lingo //
// resolve reject
// sucess failure
// fulfilled reject
// object for success {status:"fullfilled",value:"p2 success"};
// object for failue {status:"fullfilled",reason:"f2 fail"};
// all thought for all it just return values becuase it waits for all in sucess in failure simple throw error for all