-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise-and-promise-all.js
More file actions
125 lines (105 loc) · 2.98 KB
/
promise-and-promise-all.js
File metadata and controls
125 lines (105 loc) · 2.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//---------------------------------promise---------------------------------------
// let complete = false; //first test case
// let prom = new Promise( function (res, rej){
// if(complete){
// res("Hi i am resolve");
// }else{
// rej("Hi i am reject");
// }
// })
//console.log(prom(true));
// function prom(complete){ //second test case .then/.catch function
// return new Promise( function (res, rej){
// if(complete){
// res("Hi i am resolve");
// }else{
// rej("Hi i am reject");
// }
// })
// }
// let onfulfit = (result) => {
// console.log(result);
// }
// let rejfulfit = (error) => {
// console.log(error);
// }
// prom(true).then(onfulfit);
// prom(false).catch(rejfulfit);
// function prom(complete){ //third test case using setTimeout
// return new Promise( function (res, rej){
// console.log("Fetching Data Please wait");
// setTimeout(() => {
// if(complete){
// res("Hi i am resolve");
// }else{
// rej("Hi i am reject");
// }
// }, 2000)
// })
// }
// let onfulfit = (result) => {
// console.log(result);
// }
// let rejfulfit = (error) => {
// console.log(error);
// }
// prom(true).then(onfulfit);
// prom(false).catch(rejfulfit);
// prom(true).then((result) => {
// console.log(result);
// }).catch((error) => {
// console.log(error);
// });
// function prom(a,b){ //forth test case using setTimeout calculation and assign var
// return new Promise( function (res, rej){
// console.log("Fetching Data Please wait");
// var c = a/b;
// setTimeout(() => {
// if(a,b){
// res(`Hi i am resolve ${c}`);
// }else{
// rej("Hi i am reject");
// }
// }, 2000)
// })
// }
// prom(5,2).then((result) => {
// console.log(result);
// }).catch((error) => {
// console.log(error);
// });
// function prom(){ //fifth test case ajax call
// return new Promise( function (res, rej){
// console.log("Fetching Data Please wait");
// setTimeout(() => {
// $.get("https://jsonplaceholder.typicode.com/posts", (data) =>{
// console.log(data);
// })
// }, 2000)
// })
// }
// prom().then((result) => {
// console.log(result);
// }).catch((error) => {
// console.log(error);
// });
//----------------------------------Promise All-----------------------------
let p1 = new Promise((resolve, reject) => {
//if(complete){
resolve("Hi i am resolve");
// }else{
// reject("Hi i am reject");
// }
});
let p2 = new Promise((resolve, reject) => {
//if(complete){
resolve("Hi i am p2 resolve");
// }else{
// reject("Hi i am p2 reject");
// }
});
Promise.all([p1.p2]).then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});