-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathasync_await.html
More file actions
66 lines (56 loc) · 2.2 KB
/
async_await.html
File metadata and controls
66 lines (56 loc) · 2.2 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Async Await</title>
</head>
<body>
<h1>
async await
</h1>
<script>
//first, lets learn something abot a generator function. This is a special function.
// what makes a generator function are the * and yield.
//in below example, generatorFunc(), we are getting the data, pausing the function until we recieve the data and then doing something with the data.
function* generatorFunc() {
let data = getData()
yield dataconsole.log(data)
}
//USING OUR CODE FROM PROMISES
const hasMeeting = false;
const meeting = new Promise((resolve, reject) => {
if (hasMeeting) {
const meetingDetails = {
name: "Marketing Meeting",
location: "Skype",
time: "1:00PM",
printMeetingDetails: () => {
console.log(`You have a ${meetingDetails.name} at ${meetingDetails.time}. Location: ${meetingDetails.location}`)
}
}
resolve(meetingDetails)
} else {
reject(new Error("ERROR: Meeting already/not scheduled"))
}
})
const addToCalender = meetingDetails => {
const calender = `${meetingDetails.name} is scheduled at ${meetingDetails.time} on ${meetingDetails.location}`
return Promise.resolve(calender)
}
async function myMeeting() {
try {
const meetingDetails = await meeting
const message = await addToCalender(meetingDetails)
console.log(message)
} catch (err) {
console.log(err.message)
}
}
myMeeting()
// myMeeting().catch(err => console.log(err.message)) //this is an alternative for try catch, since you dont wanna do this for all function calls in a large code.
</script>
</body>
</html>