-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path38.1 async-await.html
51 lines (48 loc) · 1.43 KB
/
38.1 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
48
49
50
51
<!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>Document</title>
</head>
<body>
<!--
Study previous code before this code and also check notes
-->
<script>
console.log("Without await inside async");
async function test() {
console.log("2: message");
console.log("3: message");
console.log("4: message");
}
console.log("1: message"); // prints first
test();
console.log("5: message"); // runs last after code inside async function test()
// o/p -> 1,2,3,4,5
//Now using await
console.log("using await inside async");
async function test2() {
console.log("2: message");
await console.log("3: message : contains await!"); // it waits after printing/running 3
console.log("4: message"); //now 4 is printed after 5
}
console.log("1: message");
test2();
console.log("5: message");
// o/p-> 1,2,3,5,4
//example3
let test3 = async function () {
console.log("A");
await console.log("B");
console.log("C");
console.log("D");
};
test3();
console.log("E");
// o/p: A,B,E,C,D
//next program is example of async await with fetch()
</script>
</body>
</html>