-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasync-await.js
46 lines (39 loc) · 1.14 KB
/
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
46
// function foo(){
// return new Promise(function(resolve,reject){
// resolve("Some Data")
// })
// }
function createOrder(){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(12345)
},3000)
})
}
function proceedToPayment(){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
reject("Order created")
},3000)
})
}
async function foo(){
//When you add async keyword in front of any regular function
//It automatically returns a promise from that function
try{
const orderId=await createOrder();
console.log("Order Id",orderId)
//Pauses the Execution and waits for the promise to resolve or reeject
//After the promise is resolved the function execution continues from the point where it was left.
await proceedToPayment();
console.log("Last Statement of Async Function")
}
catch(err){
console.log("Error is",err)
}
}
//1. Await can only be used inside an async function
//Await keyword makes the function pause the execution and wait for the promise to resolve
foo()
console.log(3);
console.log(4);