Skip to content

Commit eb41a5d

Browse files
async-and-await
1 parent 04686ed commit eb41a5d

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>async-await</title>
9+
<link rel="shortcut icon" href="images/js-logo.png" type="image/x-icon">
10+
</head>
11+
12+
<body>
13+
<h1>Async and Await in JS</h1>
14+
<h2 style="font-family: cursive;">Example One:</h2>
15+
<p id="textOne" style="font-family: monospace;"></p>
16+
<h2 style="font-family: cursive;">Example Two</h2>
17+
<p id="textTwo" style="font-family: monospace;"></p>
18+
<h2 style="font-family: cursive;">Example Three</h2>
19+
<p>wait for 2 seconds after refresh...</p>
20+
<p id="textThree" style="font-family: monospace;"></p>
21+
<script src="script.js"></script>
22+
</body>
23+
24+
</html>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Asynchronous and Await Function were developed in ECMAScript 2017
3+
It is related to Promise which we discussed in previous topic
4+
'async' function is used to return Promise
5+
'await' makes function which waits for a Promise
6+
7+
First of all we will discuss about async:
8+
UnderStand the following Program
9+
Example
10+
11+
*/
12+
13+
let textOne = document.getElementById('textOne');
14+
15+
function Display(disp) {
16+
textOne.innerHTML = disp;
17+
}
18+
async function Add(a1, a2) {
19+
let x = a1 + a2;
20+
return x;
21+
}
22+
Add(22, 25).then(
23+
function(value) { Display(value) }
24+
)
25+
26+
/*
27+
Example 2 for Learning Concept of Await
28+
*/
29+
30+
let textTwo = document.getElementById('textTwo');
31+
32+
async function DisplayTwo() {
33+
let promiseTwo = new Promise(function(resolve, reject) {
34+
resolve("Resolved")
35+
});
36+
textTwo.innerHTML = await promiseTwo;
37+
}
38+
DisplayTwo();
39+
40+
// Example 3 for Learning the concept of using setTimeOut in Async-Await
41+
42+
let textThree = document.getElementById('textThree');
43+
44+
async function DisplayThree() {
45+
let promiseThree = new Promise(function(resolve) {
46+
setTimeout(function() { resolve("Resolved Display Three"); }, 2000);
47+
})
48+
textThree.innerHTML = await promiseThree;
49+
}
50+
DisplayThree();
51+
52+
53+
54+
// Example 4 for Learning the concept of Waiting for a File which was my Dream to learn
55+
// I will complete that task after learning AJAX XMLHttpRequest

0 commit comments

Comments
 (0)