Skip to content

Commit a5cb57d

Browse files
committed
Promises
1 parent dd2863c commit a5cb57d

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

app.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
// callbacks, promises, async/await
2+
// PROMISES - Pending, Resolved, Rejected
3+
// then catch - pass another callback
24

35
const heading1 = document.querySelector(".heading-1");
46
const heading2 = document.querySelector(".heading-2");
57
const heading3 = document.querySelector(".heading-3");
68
const btn = document.querySelector(".btn");
79

8-
/* Initial Example */
9-
// btn.addEventListener("click", () => {
10-
// console.log("You clicked the button.");
11-
// });
12-
13-
// console.log("I'm second");
14-
// for (let i = 0; i < 10000; i++) {
15-
// console.log("I am busy");
16-
// }
17-
18-
/* Second Example - The challenge here second one should start executing once first one completes and same goes for third to second*/
1910
btn.addEventListener("click", () => {
20-
setTimeout(() => {
21-
heading1.style.color = "red";
22-
setTimeout(() => {
23-
heading2.style.color = "green";
24-
setTimeout(() => {
25-
heading3.style.color = "blue";
26-
}, 1000);
27-
}, 2000);
28-
}, 1000);
11+
console.log("You clicked the button.");
12+
});
13+
14+
/* Promise */
15+
const promise = new Promise((resolve, reject) => {
16+
const value = false;
17+
if (value) {
18+
resolve("The value is true");
19+
} else {
20+
reject("there was a error, value is false");
21+
}
2922
});
23+
24+
promise
25+
.then((result) => {
26+
console.log(result);
27+
})
28+
.catch((error) => {
29+
console.log(error);
30+
});
31+
32+
console.log(promise);

callback-example.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// callbacks, promises, async/await
2+
3+
const heading1 = document.querySelector(".heading-1");
4+
const heading2 = document.querySelector(".heading-2");
5+
const heading3 = document.querySelector(".heading-3");
6+
const btn = document.querySelector(".btn");
7+
8+
/* Initial Example */
9+
// btn.addEventListener("click", () => {
10+
// console.log("You clicked the button.");
11+
// });
12+
13+
// console.log("I'm second");
14+
// for (let i = 0; i < 10000; i++) {
15+
// console.log("I am busy");
16+
// }
17+
18+
/* Second Example - The challenge here second one should start executing once first one completes and same goes for third to second*/
19+
btn.addEventListener("click", () => {
20+
setTimeout(() => {
21+
heading1.style.color = "red";
22+
setTimeout(() => {
23+
heading2.style.color = "green";
24+
setTimeout(() => {
25+
heading3.style.color = "blue";
26+
}, 1000);
27+
}, 2000);
28+
}, 1000);
29+
});

0 commit comments

Comments
 (0)