File tree 2 files changed +52
-20
lines changed
2 files changed +52
-20
lines changed Original file line number Diff line number Diff line change 1
1
// callbacks, promises, async/await
2
+ // PROMISES - Pending, Resolved, Rejected
3
+ // then catch - pass another callback
2
4
3
5
const heading1 = document . querySelector ( ".heading-1" ) ;
4
6
const heading2 = document . querySelector ( ".heading-2" ) ;
5
7
const heading3 = document . querySelector ( ".heading-3" ) ;
6
8
const btn = document . querySelector ( ".btn" ) ;
7
9
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
10
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
+ }
29
22
} ) ;
23
+
24
+ promise
25
+ . then ( ( result ) => {
26
+ console . log ( result ) ;
27
+ } )
28
+ . catch ( ( error ) => {
29
+ console . log ( error ) ;
30
+ } ) ;
31
+
32
+ console . log ( promise ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments