File tree 11 files changed +52
-52
lines changed
11 files changed +52
-52
lines changed Original file line number Diff line number Diff line change 2
2
3
3
// Pending
4
4
5
- const promise1 = new Promise ( resolve => {
5
+ const promise1 = new Promise ( ( resolve ) => {
6
6
setTimeout ( ( ) => {
7
7
resolve ( 'value1' ) ;
8
8
} , 0 ) ;
@@ -12,7 +12,7 @@ promise1.then(console.log); // 'value1' (delayed)
12
12
13
13
// Immediate resolve
14
14
15
- const promise2 = new Promise ( resolve => resolve ( 'value2' ) ) ;
15
+ const promise2 = new Promise ( ( resolve ) => resolve ( 'value2' ) ) ;
16
16
console . dir ( { promise2 } ) ; // Promise { 'value2' }
17
17
promise2 . then ( console . log ) ; // 'value2'
18
18
@@ -48,14 +48,14 @@ const readFile = (filename, encoding) =>
48
48
} ) ) ;
49
49
50
50
readFile ( 'file1.txt' , 'utf8' )
51
- . then ( data => {
51
+ . then ( ( data ) => {
52
52
console . log ( data ) ;
53
53
return readFile ( 'file2.txt' , 'utf8' ) ;
54
54
} )
55
- . then ( data => {
55
+ . then ( ( data ) => {
56
56
console . log ( data ) ;
57
57
return readFile ( 'file3.txt' , 'utf8' ) ;
58
58
} )
59
- . then ( data => {
59
+ . then ( ( data ) => {
60
60
console . log ( data ) ;
61
61
} ) ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const promisify = fn => ( ...args ) => new Promise ( ( resolve , reject ) => {
3
+ const promisify = ( fn ) => ( ...args ) => new Promise ( ( resolve , reject ) => {
4
4
args . push ( ( err , result ) => {
5
5
if ( err ) reject ( err ) ;
6
6
else resolve ( result ) ;
@@ -13,18 +13,18 @@ const fs = require('fs');
13
13
const readFile1 = promisify ( fs . readFile ) ;
14
14
15
15
readFile1 ( 'file1.txt' , 'utf8' )
16
- . then ( data => {
16
+ . then ( ( data ) => {
17
17
console . log ( data . toString ( ) ) ;
18
18
return readFile1 ( 'file2.txt' , 'utf8' ) ;
19
19
} )
20
- . then ( data => {
20
+ . then ( ( data ) => {
21
21
console . log ( data . toString ( ) ) ;
22
22
return readFile1 ( 'file3.txt' , 'utf8' ) ;
23
23
} )
24
- . then ( data => {
24
+ . then ( ( data ) => {
25
25
console . log ( data . toString ( ) ) ;
26
26
} )
27
- . catch ( err => {
27
+ . catch ( ( err ) => {
28
28
console . log ( err ) ;
29
29
} ) ;
30
30
@@ -33,17 +33,17 @@ const util = require('util');
33
33
const readFile2 = util . promisify ( fs . readFile ) ;
34
34
35
35
readFile2 ( 'file1.txt' , 'utf8' )
36
- . then ( data => {
36
+ . then ( ( data ) => {
37
37
console . log ( data . toString ( ) ) ;
38
38
return readFile2 ( 'file2.txt' , 'utf8' ) ;
39
39
} )
40
- . then ( data => {
40
+ . then ( ( data ) => {
41
41
console . log ( data . toString ( ) ) ;
42
42
return readFile2 ( 'file3.txt' , 'utf8' ) ;
43
43
} )
44
- . then ( data => {
44
+ . then ( ( data ) => {
45
45
console . log ( data . toString ( ) ) ;
46
46
} )
47
- . catch ( err => {
47
+ . catch ( ( err ) => {
48
48
console . log ( err ) ;
49
49
} ) ;
Original file line number Diff line number Diff line change 2
2
3
3
const fs = require ( 'fs' ) ;
4
4
5
- const readTextFile = filename => fs . promises . readFile ( filename , 'utf8' ) ;
5
+ const readTextFile = ( filename ) => fs . promises . readFile ( filename , 'utf8' ) ;
6
6
7
7
readTextFile ( 'file1-.txt' )
8
8
. then (
9
- data => {
9
+ ( data ) => {
10
10
console . dir ( { file1 : data } ) ;
11
11
return readTextFile ( 'file2.txt' ) ;
12
12
} ,
13
- reason => {
13
+ ( reason ) => {
14
14
console . log ( 'Cannot read file1.txt --- A' ) ;
15
15
console . log ( reason ) ;
16
16
}
17
17
)
18
18
. catch (
19
- reason => {
19
+ ( reason ) => {
20
20
console . log ( 'Cannot read file1.txt --- B' ) ;
21
21
console . log ( reason ) ;
22
22
}
23
23
)
24
- . then ( data => {
24
+ . then ( ( data ) => {
25
25
console . dir ( { file2 : data } ) ;
26
26
return readTextFile ( 'file3.txt' ) ;
27
27
} )
28
- . catch ( reason => {
28
+ . catch ( ( reason ) => {
29
29
console . log ( 'Cannot read file2.txt' ) ;
30
30
console . log ( reason ) ;
31
31
} )
32
- . then ( data => {
32
+ . then ( ( data ) => {
33
33
console . dir ( { file3 : data } ) ;
34
34
} )
35
- . catch ( reason => {
35
+ . catch ( ( reason ) => {
36
36
console . log ( 'Cannot read file' ) ;
37
37
console . log ( reason ) ;
38
38
} ) ;
Original file line number Diff line number Diff line change 2
2
3
3
const fs = require ( 'fs' ) ;
4
4
5
- const readTextFile = filename => fs . promises . readFile ( filename , 'utf8' ) ;
5
+ const readTextFile = ( filename ) => fs . promises . readFile ( filename , 'utf8' ) ;
6
6
7
7
readTextFile ( 'file1-.txt' )
8
- . then ( data => {
8
+ . then ( ( data ) => {
9
9
console . dir ( { file1 : data } ) ;
10
10
} )
11
- . catch ( reason => {
11
+ . catch ( ( reason ) => {
12
12
console . log ( 'Cannot read file1.txt' ) ;
13
13
console . log ( reason ) ;
14
14
} )
Original file line number Diff line number Diff line change 2
2
3
3
const http = require ( 'http' ) ;
4
4
5
- const fetch = url => new Promise ( ( resolve , reject ) => {
6
- http . get ( url , res => {
5
+ const fetch = ( url ) => new Promise ( ( resolve , reject ) => {
6
+ http . get ( url , ( res ) => {
7
7
const code = res . statusCode ;
8
8
if ( code !== 200 ) {
9
9
return reject ( new Error ( `HTTP status code ${ code } ` ) ) ;
@@ -12,7 +12,7 @@ const fetch = url => new Promise((resolve, reject) => {
12
12
res . on ( 'error' , reject ) ;
13
13
14
14
const chunks = [ ] ;
15
- res . on ( 'data' , chunk => {
15
+ res . on ( 'data' , ( chunk ) => {
16
16
chunks . push ( chunk ) ;
17
17
} ) ;
18
18
Original file line number Diff line number Diff line change 3
3
const fetch = require ( './6-fetch.js' ) ;
4
4
5
5
fetch ( 'http://localhost:3000/person' )
6
- . then ( data => {
6
+ . then ( ( data ) => {
7
7
console . log ( data ) ;
8
8
} )
9
- . catch ( err => {
9
+ . catch ( ( err ) => {
10
10
console . error ( err ) ;
11
11
} ) ;
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ const promises = [
11
11
] ;
12
12
13
13
Promise . all ( promises )
14
- . then ( values => {
14
+ . then ( ( values ) => {
15
15
console . log ( values ) ;
16
16
} )
17
- . catch ( err => {
17
+ . catch ( ( err ) => {
18
18
console . log ( err ) ;
19
19
} ) ;
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ const promises = [
11
11
] ;
12
12
13
13
Promise . race ( promises )
14
- . then ( res => {
14
+ . then ( ( res ) => {
15
15
console . log ( res ) ;
16
16
} )
17
- . catch ( err => {
17
+ . catch ( ( err ) => {
18
18
console . log ( err ) ;
19
19
} ) ;
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class Thenable {
19
19
if ( fn ) {
20
20
const next = fn ( value ) ;
21
21
if ( next ) {
22
- next . then ( value => {
22
+ next . then ( ( value ) => {
23
23
this . next . resolve ( value ) ;
24
24
} ) ;
25
25
}
@@ -29,7 +29,7 @@ class Thenable {
29
29
30
30
// Usage
31
31
32
- const readFile = filename => {
32
+ const readFile = ( filename ) => {
33
33
const thenable = new Thenable ( ) ;
34
34
fs . readFile ( filename , 'utf8' , ( err , data ) => {
35
35
if ( err ) throw err ;
@@ -39,14 +39,14 @@ const readFile = filename => {
39
39
} ;
40
40
41
41
readFile ( 'file1.txt' )
42
- . then ( data => {
42
+ . then ( ( data ) => {
43
43
console . dir ( { file1 : data } ) ;
44
44
return readFile ( 'file2.txt' ) ;
45
45
} )
46
- . then ( data => {
46
+ . then ( ( data ) => {
47
47
console . dir ( { file2 : data } ) ;
48
48
return readFile ( 'file3.txt' ) ;
49
49
} )
50
- . then ( data => {
50
+ . then ( ( data ) => {
51
51
console . dir ( { file3 : data } ) ;
52
52
} ) ;
Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ const PROMISE_TIMEOUT = 1000;
5
5
class Expirable {
6
6
constructor ( executor ) {
7
7
const promise = new Promise ( ( resolve , reject ) => {
8
- executor ( val => {
8
+ executor ( ( val ) => {
9
9
if ( this . expired ) return ;
10
10
clearTimeout ( this . timer ) ;
11
11
resolve ( val ) ;
12
- } , err => {
12
+ } , ( err ) => {
13
13
if ( this . expired ) return ;
14
14
clearTimeout ( this . timer ) ;
15
15
reject ( err ) ;
@@ -28,42 +28,42 @@ class Expirable {
28
28
29
29
// Usage
30
30
31
- new Expirable ( resolve => {
31
+ new Expirable ( ( resolve ) => {
32
32
setTimeout ( ( ) => {
33
33
resolve ( 'Resolved before timeout' ) ;
34
34
} , 100 ) ;
35
- } ) . then ( data => {
35
+ } ) . then ( ( data ) => {
36
36
console . dir ( { data } ) ;
37
- } ) . catch ( error => {
37
+ } ) . catch ( ( error ) => {
38
38
console . dir ( { error : error . message } ) ;
39
39
} ) ;
40
40
41
41
new Expirable ( ( resolve , reject ) => {
42
42
setTimeout ( ( ) => {
43
43
reject ( new Error ( 'Something went wrong' ) ) ;
44
44
} , 100 ) ;
45
- } ) . then ( data => {
45
+ } ) . then ( ( data ) => {
46
46
console . dir ( { data } ) ;
47
- } ) . catch ( error => {
47
+ } ) . catch ( ( error ) => {
48
48
console . dir ( { error : error . message } ) ;
49
49
} ) ;
50
50
51
- new Expirable ( resolve => {
51
+ new Expirable ( ( resolve ) => {
52
52
setTimeout ( ( ) => {
53
53
resolve ( 'Never resolved before timeout' ) ;
54
54
} , 2000 ) ;
55
- } ) . then ( data => {
55
+ } ) . then ( ( data ) => {
56
56
console . dir ( { data } ) ;
57
- } ) . catch ( error => {
57
+ } ) . catch ( ( error ) => {
58
58
console . dir ( { error : error . message } ) ;
59
59
} ) ;
60
60
61
61
new Expirable ( ( resolve , reject ) => {
62
62
setTimeout ( ( ) => {
63
63
reject ( new Error ( 'Never rejected before timeout' ) ) ;
64
64
} , 2000 ) ;
65
- } ) . then ( data => {
65
+ } ) . then ( ( data ) => {
66
66
console . dir ( { data } ) ;
67
- } ) . catch ( error => {
67
+ } ) . catch ( ( error ) => {
68
68
console . dir ( { error : error . message } ) ;
69
69
} ) ;
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ const server = http.createServer((req, res) => {
40
40
return ;
41
41
}
42
42
43
- handler ( req , result => {
43
+ handler ( req , ( result ) => {
44
44
const json = JSON . stringify ( result ) ;
45
45
res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
46
46
res . end ( json ) ;
You can’t perform that action at this time.
0 commit comments