-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAsync-Await.js
More file actions
231 lines (208 loc) · 6.16 KB
/
Copy pathAsync-Await.js
File metadata and controls
231 lines (208 loc) · 6.16 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* 💡"JavaScript-with-JC"
👉 Async-await Modern approach for handling Promises
The async and await keywords provide a way to handle the asynchronous tasks in a synchronous manner.
async/await allows promise-based behavior to be written in a cleaner way,
💡We can handle promises using async await :-
1) Sequentially
2) Concurrently
*/
// 💡Let's take a simple Example -
// generateNumber 👇 is a promise which is created by promise constructor
function simpleExample() {
const generateNumber = new Promise((resolve, reject) => {
console.log("generating number...");
setTimeout(() => {
let number = Math.floor(Math.random() * 10);
if (number >= 5) {
resolve(`number generated successfully ${number}.`);
} else {
reject("problem in generating number!");
}
}, 1000);
});
// handling promise in a cleaner way using async await
const doTask = async () => {
try {
const result = await generateNumber;
console.log(result);
} catch (error) {
console.log(error);
} finally {
console.log("Promise is settled, either fulfilled or rejected");
}
};
doTask();
}
// simpleExample()
// 💡 1) Sequential Async await
function sequentialAsyncAwait() {
const addItmesToCart = () => {
return new Promise((resolve, reject) => {
const items = [
{ id: 1, name: "Shoe", price: 2999 },
{ id: 1, name: "T-shirt", price: 1499 },
];
setTimeout(() => {
if (items) {
resolve("Items added Successfully to the cart!");
} else {
reject("Your cart is empty, Please add Items!");
}
}, 1000);
});
};
const addShippingAddress = () => {
const address = "L-3, xyz, India";
return new Promise((resolve, reject) => {
setTimeout(() => {
if (address) {
resolve("Address added Successfully!");
} else {
reject("No address added!");
}
}, 1000);
});
};
const makePayment = () => {
const paymentStatus = true;
return new Promise((resolve, reject) => {
setTimeout(() => {
if (paymentStatus) {
resolve("Payment Successfull!");
} else {
reject("Payment Failed!");
}
}, 1000);
});
};
const orderSummary = () => {
const orderID = "56881";
return new Promise((resolve, reject) => {
setTimeout(() => {
if (orderID) {
resolve(`Order Summary, ID: ${orderID}!`);
} else {
reject("Order Summary Failed!");
}
}, 1000);
});
};
// handling promises using async await sequentially
const placingOrder = async () => {
try {
const cartResult = await addItmesToCart();
console.log(cartResult); // Items added Successfully to the cart!
const shippingResult = await addShippingAddress();
console.log(shippingResult); // Address added Successfully!
const paymentResult = await makePayment();
console.log(paymentResult); // Payment Successfull!
const orderResult = await orderSummary();
console.log(orderResult); // Order Summary, ID: 56881!
} catch (error) {
console.log(error);
}
};
placingOrder();
}
// sequentialAsyncAwait();
// 💡 2) Concurrent Async await
function concurrentAsyncAwait() {
const taskOne = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
let status = true;
if (status) {
resolve("Task 1 Succeed");
} else {
reject("Task 1 Failed");
}
}, 1000);
});
};
const taskTwo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
let status = true;
if (status) {
resolve("Task 2 Succeed");
} else {
reject("Task 2 Failed");
}
}, 2000);
});
};
const taskThree = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
let status = true;
if (status) {
resolve("Task 3 Succeed");
} else {
reject("Task 3 Failed");
}
}, 3000);
});
};
const taskFour = () => {
return new Promise((resolve, reject) => {
resolve("Task 4 Succeed");
});
};
// handling promises using async await concurrently
const doTasks = async () => {
const beforeTime = new Date();
try {
// Starts all tasks immediately
const task1 = taskOne();
const task2 = taskTwo();
const task3 = taskThree();
const task4 = taskFour();
const task4Result = await task4;
console.log(task4Result); // Task 4 Succeed almost instantly.
const task3Result = await task3;
console.log(task3Result); // Task 3 Succeed after 3 sec.
const task2Result = await task2;
console.log(task2Result); // Task 2 Succeed immediately after Task 3, since Task 2 is already resolved.
const task1Result = await task1;
console.log(task1Result); // Task 1 Succeed immediately after Task 2, since Task 1 is already resolved.
const afterTime = new Date();
console.log("Time Taken", afterTime - beforeTime); // Time Taken 3011 => "Only 3 Sec rather than 6 Sec( In case of Sequentially )"
} catch (error) {
console.log(error);
}
};
doTasks();
}
// concurrentAsyncAwait();
// 💡 convert promise then catch code into async await code
import fetch from "node-fetch";
function fetchUsersList() {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => response.json())
.then((data) => {
const userNames = data.map((user) => {
return user.name;
});
console.log("userNames", userNames);
})
.catch((error) => {
console.log("error", error);
});
}
fetchUsersList();
async function fetchUsersListUsingAsyncAwait() {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
if (!response.ok) {
throw new Error("Something went wrong");
}
const data = await response.json();
const userNames = data.map((user) => {
return user.name;
});
console.log("userNames Async-await", userNames);
} catch (error) {
console.log("error", error);
}
}
fetchUsersListUsingAsyncAwait();