-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromises.js
155 lines (135 loc) · 3.55 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* PROMISES--> to overcome the issue of callback hells.
Promise is for "eventual" completion of task
resolve and reject are callbacks provided by JS
** 3 STATES OF PROMISES **
1- Pending --> no final result is received/ the result is undefined
2- Fulfilled --> final result is received/ the result is a value (fulfilled)
3- Rejected --> an error occured and result can't be generated/ the result is an error object
Generally, we do not create promises, they are generated by APIs, and we need to handle them
*/
let promise = new Promise((resolve, reject) => {
console.log("Promise");
resolve("Success"); // to fulfill promise
// reject("Rejected"); //to generate error
});
/*
** state will be pending for this even when the data is printed coz we haven't resolved it **
let getData = (dataId, getNextData) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data", dataId);
if (getNextData) {
getNextData();
}
}, 5000);
});
};
*/
//RESOLVING CALLBACK HELL USING PROMISES
let getData = (dataId, getNextData) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data", dataId);
resolve("Success");
if (getNextData) {
getNextData();
}
}, 3000);
});
};
// PROMISE CHAIN
getData(1).then((res)=>{
console.log(res);
getData(2).then((res) => {
console.log(res);
});
});
//Another way of writing Promise Chain or known as chain of .then
getData(1)
.then((res) => {
return getData(2);
})
.then((res) => {
return getData(3);
})
.then((res) => {
console.log(res);
});
// // *************************************************************************** //
/* HOW TO USE PROMISES
.then()--> fulfill hony ky baad koi kaam krawana
.catch()--> reject hony ky baad koi kaam krawana */
const getPromise = () => {
return new Promise((resolve, reject) => {
console.log("Promise");
// resolve("Success");
reject("Failure"); //msg for rejection will be written in parentheses
});
};
// For Resolve Promise
let promisee = getPromise();
promisee.then((res)=>{
console.log("Promise fulfilled", res); //this won't work in case of reject promise
});
// For Reject Promise
promisee.catch((err)=>{
console.log("Promise rejected", err);
});
// *************************************************************************** //
function asyncFun1() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Data1");
resolve("Success!");
}, 2000);
});
}
function asyncFun2() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Data2");
resolve("Success!");
}, 2000);
});
}
{
console.log("Fetching Data1...");
let p1= asyncFun1();
p1.then((res)=>{
console.log(res);
});
console.log("Fetching Data2...");
let p2= asyncFun2();
p2.then((res)=>{
console.log(res);
});
}
/* OUTPUT
Fetching Data1...
Fetching Data2...
Data1
Success!
Data2
Success!
*/
/*****************************************************************/
//WE WANT THAT ONCE THE EXECUTION FOR DATA 1 IS COMPLETED THEN DATA 2 STARTS TO EXECUTE. FOR THAT WE CAN DO CHAINING
console.log("Fetching Data1...");
let p1 = asyncFun1();
p1.then((res) => {
console.log(res);
console.log("Fetching Data2...");
let p2 = asyncFun2();
p2.then((res) => {
console.log(res);
});
});
// To Simplify Code
console.log("Fetching Data1...");
asyncFun1().then((res) => {
console.log(res);
console.log("Fetching Data2...");
asyncFun2().then((res) => {
console.log(res);
});
});