-
Notifications
You must be signed in to change notification settings - Fork 0
/
Async & Await in JavaScript.js
40 lines (33 loc) · 1.41 KB
/
Async & Await in JavaScript.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
/* There is a special syntax to work with pronous in JavaScript. A function can be made async by using async keyword.
An async function always returns a promise, other values are wrapped in a promise automatically.
The await keyword makes javascript wait untill the promise settles and returns its value. Its just a more elegant syntax of getting the promise result.*/
async function harry() {
let delhiWeather = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("27 Deg")
}, 2000)
})
let bangaloreWeather = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("21 Deg")
}, 5000)
})
// delhiWeather.then(alert)
// bangaloreWeather.then(alert)
console.log("Fetching Delhi Weather Please wait ...")
let delhiW = await delhiWeather
console.log("Fetched Delhi Weather: " + delhiW)
console.log("Fetching Bangalore Weather Please wait ...")
let bangaloreW = await bangaloreWeather
console.log("Fetched Bangalore Weather: " + bangaloreW)
return [delhiW, bangaloreW]
}
const cherry = async () => {
console.log("Hey I am cherry and I am waiting ")
}
const main1 = async () => {
console.log("Welcome to weather control room")
let a = await harry()
let b = await cherry()
}
main1()