-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-async-await.js
45 lines (36 loc) · 1.21 KB
/
5-async-await.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
// async await in javascript
/*
- there is way to improve handling asynchronous task even further
- by using async/await keywords introduced in ES2017(ES8)
- the async await keywords allow us to write completely synchronous looking
- code while performing asynchronous tasks behind the scenes.
*/
/* async
- the async keyword is used to declare async functions
- async functions are functions that are instances of the AsyncFunction constructor
- Unlike normal functions, async functions always return a promise
*/
// normal function
function normalFunc() {
return 'hello'
}
console.log(normalFunc()) // hello
// async function which returns promise
async function asyncFunc() {
return 'world'
}
asyncFunc()
.then((result) => console.log(result))
/* await
- await keyword can be put infront of any async promise based function to pause
- the code until that promise settles and returns it's results
- await only works inside async functions. cannot use await in normal functions
*/
async function greet(){
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Hello world!'), 1000)
})
let result = await promise; // wait until the promise resolves
console.log(result) // Hello world!
}
greet();