-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.js
More file actions
153 lines (118 loc) · 3.57 KB
/
async.js
File metadata and controls
153 lines (118 loc) · 3.57 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
// async function getData(){
// return "async";
// }
// const data=getData();
// console.log(data);
// async function getData(){
// return "shalu";
// }
// const dataPromise=getData();
// dataPromise.then(res=>console.log(res));
// const p=new Promise((resolve,reject)=>{
// resolve("Promise Resolved Value!!");
// });
// async function getData(){
// return p;
// }
// getData().then(console.log);
// const dataPromise=getData();
// dataPromise.then(res=>console.log(res));
//Using async await
//await can only be used wihtin the async function only
// const p=new Promise((resolve,reject)=>{
// setTimeout(()=>{
// resolve("Promise Resolved Value!!");
// },10000);
// });
// async function handlePromise(){
// const val=await p;
// console.log(val);
// }
// handlePromise();
//without using async await
// const p=new Promise((resolve,reject)=>{
// setTimeout(()=>{
// resolve("Promise Resolved Value!!");
// },10000);
// });
// function getData(){
// //JS Engine will not wait for promise to be resolved.
// p.then((res)=>console.log(res));
// console.log("Hello World!");
// }
// getData();
//using async await!
// const p=new Promise((resolve,reject)=>{
// setTimeout(()=>{
// resolve("Promise Resolved Value!!");
// },10000);
// });
// async function handlePromise(){
// console.log("Hi!")
// //JS Engine was waiting for the promise to be resolved.
// const val=await p;//code was waiting.
// console.log("Hello World!")
// console.log(val);
// }
// handlePromise();
// const p=new Promise((resolvereject)=>{
// setTimeout(()=>{
// resolvereject("Promise Resolved Value!!");
// },10000);
// });
// async function handlePromise(){
// console.log("Hi!")
// //JS Engine was waiting for the promise to be resolved.
// const val=await p;//code was waiting.
// console.log("Hello World!")
// console.log(val);
// //after 10sec both the promises printed
// const val2=await p;//code was waiting.
// console.log("Hello World2!")
// console.log(val2);
// }
// handlePromise();
const p1=new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("Promise got resolved1!!");
},10000);
});
const p2=new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("Promise got resolved2!!");
},5000);
});
async function handlePromise(){
console.log("Hi!")
//JS Engine waits for the promise to be resolved.
const val=await p2;
console.log("Hello World1!")
console.log(val);
const val2=await p1;
console.log("Hello World2!")
console.log(val2);
}
handlePromise();
// Empty call stack
// call satck-a place where every function works.
// There are two promises p1,p2.Async promises.
// First way of handling errors using Try catch blocks
// const API_URL="https://api.github.com/users/akshaymarch7";
// async function handlePromise(){
// try{
// const data=await fetch(API_URL);//when you fetch it gives you a promise
// const jsonValue=await data.json();//fetch()=>Response.json()=>jsonValue
// console.log(jsonValue);
// }catch(err){
// console.log(err);
// }
// }
// handlePromise();
// Second way of handling errors is using only catch
// const API_URL="https://api.github.com/users/akshaymarch7";
// async function handlePromise(){
// const data=await fetch(API_URL);//when you fetch it gives you a promise
// const jsonValue=await data.json();//fetch()=>Response.json()=>jsonValue
// console.log(jsonValue);
// }
// handlePromise().catch(err =>console.log(err));