Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
180 lines (156 loc) · 2.96 KB
/
Copy pathapp.js
File metadata and controls
180 lines (156 loc) · 2.96 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
// Concept 3: Async-Await
// async function always return promise
/*
// A. Basic example
async function hello() {
console.log("hello there!");
}
// B. Real world analogy
function api() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("weather data");
resolve("bye");
}, 2000);
});
}
async function getWeatherData() {
await api();
}
*/
// C. More clarified version
function getData(dataId) {
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data", dataId);
resolve("Response");
}, 2000);
});
}
//Async-await
async function getAllData() {
await getData(1);
await getData(2);
await getData(3);
await getData(4);
}
// Concept 2 : Promises
// A. Promise Understanding
/*
let promise = new Promise((resolve, reject) => {
console.log("I am a Promise");
reject("fatal error!");
});
*/
// B. Promise example
/*
const getPromise = () => {
return new Promise((resolve, reject) => {
console.log("I am promise");
resolve("success!");
// reject("some error");
});
};
// Object of Promise
let promise = getPromise();
promise.then((res) => {
console.log("promise fulfilled", res);
});
promise.catch((err) => {
console.log("rejected", err);
});
*/
// Promise chain (Example - #1)
/*
function asyncFunc1() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data 1");
resolve("success");
}, 5000);
});
}
function asyncFunc2() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data 2");
resolve("success");
}, 4000);
});
}
console.log("fetching data 1....");
asyncFunc1().then((res) => {
// console.log(res);
console.log("fetching data 2....");
asyncFunc2().then((res) => {
// console.log(res);
});
});
*/
// 2. Promise chain (Example - #1)
/*
function getData(dataId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("data", dataId);
// resolve("Fulfilled");
resolve("success");
}, 5000);
});
}
console.log("Fetching data 1...");
getData(1)
.then((res) => {
return getData(2);
})
.then((res) => {
return getData(3);
})
.then((res) => {
console.log(res);
});
*/
// Concept 1: Callbacks hell / Nested Callbacks
/*
function getData(dataId, getNextData) {
return new Promise((resolve, reject) => {
setTimeout(() => {
// console.log("data", dataId);
// resolve("Fulfilled");
reject("Error");
if (getNextData) {
getNextData();
}
}, 5000);
});
*/
// Callback hell - nested callbacks
/*
getData(1, () => {
console.log("getting data 2 .... ");
getData(2, () => {
console.log("getting data 3 .... ");
getData(3, () => {
console.log("getting data 4 .... ");
getData(4);
});
});
});
*/
// setTimeout()
/* const hello = () => {
console.log("hello");
};
setTimeout(hello, 4000); */
// Callback Example
/*
function sum(a, b) {
console.log(a + b);
}
function calculator(a, b, sumCallback) {
sumCallback(a, b);
}
calculator(1, 2, (a, b) => {
console.log(a + b);
});
̀*/