-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (47 loc) · 1.35 KB
/
app.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
47
48
49
50
51
52
53
54
// callbacks, promises, async/await
// must have async to use await
// await waits till promise is settled i.e either resolved or rejected
// error handling - try/catch
// async function by itself always returns a promise so we can use .then on it
const heading1 = document.querySelector(".heading-1");
const heading2 = document.querySelector(".heading-2");
const heading3 = document.querySelector(".heading-3");
const btn = document.querySelector(".btn");
/* Async/Await Syntax */
// async function someFunction() {
// await
// }
// const otherFunction = async () => {
// await
// };
btn.addEventListener("click", async () => {
// displayColor().then((data) => {
// console.log(data);
// });
const result = await displayColor();
console.log(result);
});
/* This returns a promise */
async function displayColor() {
try {
const first = await addColor(1000, heading1, "red");
await addColor(2000, heading2, "green");
await addColor(1000, heading3, "blue");
console.log(first);
} catch (error) {
console.log(error);
}
return "hello";
}
function addColor(time, element, color) {
return new Promise((resolve, reject) => {
if (element) {
setTimeout(() => {
element.style.color = color;
resolve();
}, time);
} else {
reject(new Error(`There is no such element ${element}`));
}
});
}