-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay23.js
More file actions
174 lines (137 loc) · 5.23 KB
/
Day23.js
File metadata and controls
174 lines (137 loc) · 5.23 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
// EP-23 | Async await
// What is async?
// What is await?
// How async await works behind the scenes?
// Examples of using async await
// Error handling
// Interviews
// Async await vs Promise.then().catch()
// Intro:
// async is a keyword that is used before a function to create a async function.
// An async function always returns a promise
async function foo(params) {
// There are 2 things which i can return from this function
// 1. A promise can be returned from here.
// 2. A value can also be returned from here but this function will wrap it by a promise before returning.
// So, thing is that this function will always return a promise.
console.log(params);
}
async function getData() {
return "Namaste";
}
const data = getData();
console.log(data);
// Output:
// [[Prototype]]: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: "Namaste"
data.then((res) => console.log(res));
const promise = new Promise((resolve, reject) => {
resolve("Promise resolved");
});
async function returnPromise(params) {
return promise;
}
const promiseData = returnPromise();
console.log(promiseData);
const promiseDataWithoutObject = returnPromise().then((res) =>
console.log(res)
);
// PS: async and await combo is used to handle promises.
// Before async/await how did we handled the promises.
function fetchData() {
promise.then((res) => console.log(res));
}
fetchData();
// After async/await
async function handlePromise(params) {
const value = await promise;
console.log(value);
}
handlePromise();
// PS: await is a keyword that can be only used inside an async function
// Case study:
const mockApi = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved!!!");
}, 5000);
});
function olderWayFetchData() {
// JS Engine will not wait for promise to be resolved
mockApi.then((res) => console.log(res));
console.log("Namaste JS");
}
olderWayFetchData();
// Output:
// Namaste JS
// Promise resolved
async function newWayHandlePromise(params) {
console.log("Hello world");
// JS engine was waiting here for the promise to be resolved
// This is the only exception where JS waits for someone (Just to remember BTS it does not happen)
// It looks like the JS engine is waiting but it is not true at all.
// The main thread will be blocked for this function
const value = await mockApi;
console.log("Namaste JS");
console.log(value);
// Once a promise is resolved JS will not wait for the same promise again in the same function.
// and it will get executed instantly
const value2 = await mockApi;
console.log("Namaste JS");
console.log(value2);
}
newWayHandlePromise();
const mockApi2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved!!!");
}, 5000);
});
const mockApi3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved!!!");
}, 10000);
});
async function newWayHandlePromise2(params) {
console.log("Hello world");
// JS engine was waiting here for the promise to be resolved
// This is the only exception where JS waits for someone
// The main thread will be blocked for this function
const value = await mockApi2;
console.log("Namaste JS");
console.log(value);
// Once a promise is resolved JS will not wait for the same promise again in the same function.
// and it will get executed instantly
const value2 = await mockApi3;
console.log("Namaste JS 2");
console.log(value2);
// If a single promise is getting resolved 2 times in a function then the engine will resolve it once
// and cache it for the second line.
// Behavior of this function:
// When this function will be invoked the JS engine will go line by line for execution because JS is a
// synchronous single threaded language. Now this function is inside our call stack and everything will
// start getting executed one by one. Hello world will be logged, now promise will be encountered and
// this function will be suspended and will move out of the call stack so that it cannot block the
// main thread. It will wait till first mentioned promise is resolved and then it will move ahead and
// then it will move to call stack and it will start executing again from where it left the last execution
// Namaste JS and value will be printed.
// Now new promise will get encountered and same process will be followed again. Then Namaste JS 2 and value2
// will be printed.
// PS: JS engine is not waiting here for the promise to be resolved it just suspend the execution of the
// async function and move ahead till the line executes and then when promise gets resolved again start
// from the last place where it left of.
}
newWayHandlePromise2();
async function usingFetch(params) {
// const data = await fetch(""); // returns a response object
// this response object has a body which is a readable stream.
// if we want to convert this readable stream to json.
// response.json() can be used, this res.json() will return a promise
// when this promise is resolved it will give us the result.
// const jsonValue = await data.json();
// console.log(jsonValue);
try {
const data = await fetch();
const jsonValue = await data.json();
console.log(jsonValue);
} catch (error) {
console.log(error);
}
}