-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
120 lines (86 loc) · 2.61 KB
/
async.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
/* Async */
//async function always returns a promise, it works like promise only but you dont have to declare RESOLVE and REJECT in functon
//syntax:-
//add async in the start of the function
//example:-
/*
async function test(){
}; */
//example for async
/*
async function test(){
return "hello";
}
test().then((response)=>{
console.log(response);
});
*/
/*
let test= async function(){
return "hello";
}
test().then((response)=>{
console.log(response);
});
*/
/*
let test= async ()=>
"hello";
test().then((response)=>{
console.log(response);
});
*/
//example for await(use await when we want to fetch the data from the server)(it works under the async function)(when await is called it stop the execution of that particular line/statement and will move to the next line and will complete the full execution and then it will execute that await line/statement)
//example of await
/*
async function test(){
console.log("two");
await console.log("three");
console.log("four");
};
console.log("one");
test();
console.log("five");
*/
//example of await with fetch("")
/*
async function test(){
console.log("two");
const response= await fetch(`https://randomuser.me/api/
`);
console.log("three");
const people= await response.json();
return people;
};
console.log("one");
let a=test();
console.log("four");
console.log(a);
*/
/*
async function test(){
const response= await fetch(`https://randomuser.me/api/`);
const people= await response.json();
return people;
}
test().then((data)=>{
console.log(data);
}).catch((error)=>{
console.log(error);
});
*/
//example using try and catch
/*
async function test(){
try{
const response = await fetch(`https://randomuser.me/api/`);
const students = await response.json();
return students;
}catch(error){
console.log(error);
}
}
test().then((dat)=>{
console.log(dat);
});
*/