File tree 4 files changed +97
-10
lines changed
4 files changed +97
-10
lines changed Original file line number Diff line number Diff line change 1
1
< html >
2
2
< head >
3
3
< title > Dummy page</ title >
4
- < script src ="lesson3-async/index2 .js "> </ script >
4
+ < script src ="lesson3-async/index3 .js "> </ script >
5
5
</ head >
6
- < body > </ body >
7
- < script src ="lesson3-async/index2-script.js "> </ script >
6
+ < body >
7
+ < button id ="btn "> Click me</ button >
8
+ < button id ="btn2 "> Second button</ button >
9
+ </ body >
8
10
</ html >
Original file line number Diff line number Diff line change 1
1
// Callbacks
2
+ function main ( ) {
3
+ const btn = document . getElementById ( "btn" ) ;
4
+ btn . addEventListener ( "click" , clickButton ) ;
5
+ }
2
6
3
- function loadScript ( src ) {
4
- // creates a <script> tag and append it to the page
5
- // this causes the script with given src to start loading and run when complete
6
- let script = document . createElement ( "script" ) ;
7
- script . src = src ;
8
- document . head . append ( script ) ;
7
+ function clickButton ( ) {
8
+ console . log ( "%c btn clicked -> " , "background: #222; color: royalblue" ) ;
9
9
}
10
10
11
- loadScript ( "/my/script.js" ) ; // the script has "function sampleFn() {…}"
11
+ const arr = [ 1 , 2 , 3 ] ;
12
+
13
+ const newArr = arr . map ( ( a ) => a * 2 ) ;
14
+ console . log ( newArr ) ;
Original file line number Diff line number Diff line change
1
+ console . log ( "Page loading" ) ;
2
+ console . log ( "%c document -> " , "background: #222; color: royalblue" , document ) ;
3
+
4
+ function clickButton ( number ) {
5
+ console . log (
6
+ `%c btn ${ number } clicked -> ` ,
7
+ "background: #222; color: royalblue"
8
+ ) ;
9
+ }
10
+
11
+ window . addEventListener ( "load" , ( ) => {
12
+ console . log ( "Page loaded" ) ;
13
+ const btn = document . getElementById ( "btn" ) ;
14
+ btn . addEventListener ( "click" , clickButton ) ;
15
+ btn . removeEventListener ( "click" , clickButton ) ;
16
+
17
+ const btn2 = document . getElementById ( "btn2" ) ;
18
+ btn2 . addEventListener ( "click" , clickButton ) ;
19
+ } ) ;
Original file line number Diff line number Diff line change
1
+ function asyncRequest ( str ) {
2
+ const p = new Promise ( ( res , rej ) => {
3
+ if ( str . includes ( "please" ) ) {
4
+ res ( str ) ;
5
+ } else {
6
+ rej ( new Error ( "Be pollite!" ) ) ;
7
+ }
8
+ } ) ;
9
+ return p ;
10
+ }
11
+
12
+ function main ( ) {
13
+ const req = asyncRequest ( "Take please" ) ;
14
+ console . log ( 1 ) ;
15
+ req . then ( ( res ) => {
16
+ console . log ( `Success!, ${ res } ` ) ;
17
+ return "New Then!" ;
18
+ } )
19
+ // .then((res) => {
20
+ // console.log(res);
21
+ // return "New Then2";
22
+ // })
23
+ . catch ( ( err ) => {
24
+ console . log ( `Fail! ${ err . message } ` ) ;
25
+ return Promise . reject ( err ) ;
26
+ } )
27
+ // .catch((err) => {
28
+ // console.log(`Fail! ${err.message}`);
29
+ // })
30
+ // .then((res) => {
31
+ // console.log(res);
32
+ // })
33
+ . finally ( ( ) => {
34
+ console . log ( "Finally" ) ;
35
+ } ) ;
36
+ console . log ( 2 ) ;
37
+ }
38
+
39
+ async function mainAsyncAwait ( ) {
40
+ console . log ( 1 ) ;
41
+ try {
42
+ // const takeRes = await asyncRequest("Take please");
43
+ // const giveRes = await asyncRequest("Give please");
44
+ // const [takeRes, giveRes] = await Promise.all([
45
+ // asyncRequest("Take please"),
46
+ // asyncRequest("Give please"),
47
+ // ]);
48
+ // const pr1 = asyncRequest("Take please");
49
+ // const pr2 = asyncRequest("Give please");
50
+
51
+ // const takeRes = await pr1;
52
+ // const giveRes = await pr2;
53
+ console . log ( `Success!, ${ takeRes } ` ) ;
54
+ console . log ( `Success!, ${ giveRes } ` ) ;
55
+ } catch ( err ) {
56
+ console . log ( `Fail! ${ err . message } ` ) ;
57
+ } finally {
58
+ }
59
+ console . log ( 2 ) ;
60
+ }
61
+
62
+ // main();
63
+ mainAsyncAwait ( ) ;
You can’t perform that action at this time.
0 commit comments