-
Notifications
You must be signed in to change notification settings - Fork 3
/
38 Async-Await in JS.html
40 lines (36 loc) · 1.26 KB
/
38 Async-Await in JS.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Async/Await in JS</title>
</head>
<body>
<script>
// check notes for basic info
//->async-await makes asynchronous programming easy
//->it handles promises easily and in proper manner
//->it always returns a promise
//normal function :
// function test(){
// return "Hello";
// }
// console.log(test()); // hello
//async funciton:
async function test() { // or let test = async function(){...}
return "hello async";
}
//console.log(test()); //returns promise :o/p -> Promise {<fulfilled>: 'hello async'}
//here we don't need to write resolve and reject we can directly call then and catch:
//if test() function runs successfully then this then() is going to call automatically
test().then((result) => {
//the return value of function gets stored in result
console.log(result);
});
//async in arrow function
let test2 = async () => "async inside arrow function";
test2()
.then((data) => console.log(data))
.catch((e) => console.log("error:",e));
//in next code we will be looking at both async-await methods together
</script>
</body>
</html>