-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-await.html
47 lines (37 loc) · 1.52 KB
/
async-await.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advance JS</title>
<p> Async: It simply allows us to write promises based code as if it was synchronous and it checks that we are not breaking the execution thread. It operates asynchronously via the event-loop. Async functions will always return a value. It makes sure
that a promise is returned and if it is not returned then javascript automatically wraps it in a promise which is resolved with its value</p>
<p>Await: Await function is used to wait for the promise. It could be used within the async block only. It makes the code wait until the promise returns a result. It only makes the async block wait.</p>
<script>
// Async
async function test() {
return "hello";
}
test().then((result) => {
console.log(result);
});
// Await
//( mostly used when data is getting from server)
// fetch function
async function test1() {
console.log("2: Message");
const response = await fetch("student_data.json");
console.log("3: Message");
const students = await response.json();
return students;
}
console.log("1: Message");
let a = test1();
console.log("4: Message");
console.log(a);
</script>
</head>
<body>
</body>
</html>