-
Notifications
You must be signed in to change notification settings - Fork 9
/
asyncDemo.js
153 lines (130 loc) · 4.28 KB
/
asyncDemo.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
// Synchronus JS = a code is executed line by line
// Default behaviour of JS
// console.log("Javascript is Fun");
// console.log("I like JS");
// console.log("But I need to learn more");
// --- Asynchronous JS ----
// Approach 1: use setTimeout()
// It mimics waiting for something to happen
// Waiting for a pizza to bake
// console.log("Order Pizza");
// setTimeout(() => {
// console.log("Your pizza is now ready");
// }, 10000 ) // time is represented in ms
// console.log("Let's prepare drinks!");
// console.log("Lets Partyy!!!");
// clearTimeout()
// when the user wants to stop the execution of the function before the time, not mandatory only used
// when/if required
// ---- using setInterval() and clearInterval() ----
// Pizza Countdown Example
// Set a countdown time (in secs)
// let countdown = 5;
// function pizzaCountDown(){
// if (countdown > 0){
// console.log("Pizza will be ready in " + countdown + "seconds");
// countdown--;
// }
// else{
// console.log("Your pizza is ready 🍕");
// clearInterval(timer); // to stop execution, to avoid any performance issues, unusual behavior
// }
// }
// // pizzaCountDown();
// const timer = setInterval(pizzaCountDown, 1000) // 1000ms = 1secs
// Callback Functions -
// A function that is passed as an argument to another function and
// gets called after an operation is finished
// Example:
// 1. When the pizza is ready --> bakePizza()
// 2. serve it to guests ---> servePizza()
// 3. Call the functions
// 1st Function
// function bakePizza(callback){
// console.log("Baking Pizza....");
// // after 5secs delay show that pizza is ready
// setTimeout(() => {
// console.log("Pizza is ready to eat!!!");
// callback();
// }, 5000);
// console.log("Prepare some drinks");
// }
// // 2nd Function
// function servePizza(){
// console.log("Serving the pizza to the guests");
// }
// // Calling the function
// bakePizza(servePizza);
// ---------- Week 6 - Day 2 -----------
// ------------- PROMISES --------
// Scenario: You order a pizza and the store promises it will be delivered.
// You can then do something based on whether the promise was fulfilled (resolved - pizza arrived) or rejected (cancel order - refund)
// Creating Promise
// let orderPizza = new Promise( (resolve, reject) => {
// let pizzaDelivered = true;
// setTimeout( () =>{
// if(pizzaDelivered){
// resolve("Pizza is delivered to you!")
// }
// else{
// reject("Not delivered, sorry");
// }
// }, 5000 )
// })
// //Handling Promise
// orderPizza
// .then ( (message) => console.log(message)) // It shows the resolve message
// .catch( (error) => console.log(error)) // It displays the error message
// .finally (() => console.log("All settled successfully"));
// console.log("Heey I am learning promises today");
// console.log("Its my last day of JS")
// ----- PROMISES CHAINING -----
// STEP 1: Order pizza (returns a promise)
// const orderPizza = function () {
// return new Promise((resolve, reject) => {
// let pizzaDelivered = true;
// setTimeout(() => {
// if (pizzaDelivered) {
// resolve("Pizza is delivered to you!");
// } else {
// reject("Not delivered, sorry");
// }
// }, 1000);
// });
// };
// // STEP 2: Serve pizza (returns a promise)
// const servePizza = function(){
// return new Promise((resolve) =>{
// setTimeout( () =>{
// resolve("Pizza is served to the guests!")
// }, 2000);
// })
// }
// // STEP 3: Clean Up (returns a promise)
// const cleanUp = function(){
// return new Promise((resolve) =>{
// setTimeout( () =>{
// resolve("Cleaned Up after the party!")
// }, 2000);
// })
// }
// // Chain the promises together
// orderPizza()
// .then((message) => {
// console.log(message);
// return servePizza(); // returns the next promise
// })
// .then((message) =>{
// console.log(message);
// return cleanUp(); // returns the next promise
// })
// .then((message) => {
// console.log(message);
// })
// .catch((error) =>{
// console.log(error); // Handle any error from any step
// })
// .finally(() => {
// console.log("Partyy Over!!!!")
// })
// ------ using Fetch API -------