File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33// Pending
44
5- const promise1 = new Promise ( resolve => {
5+ const promise1 = new Promise ( ( resolve ) => {
66 setTimeout ( ( ) => {
77 resolve ( 'value1' ) ;
88 } , 0 ) ;
@@ -12,7 +12,7 @@ promise1.then(console.log); // 'value1' (delayed)
1212
1313// Immediate resolve
1414
15- const promise2 = new Promise ( resolve => resolve ( 'value2' ) ) ;
15+ const promise2 = new Promise ( ( resolve ) => resolve ( 'value2' ) ) ;
1616console . dir ( { promise2 } ) ; // Promise { 'value2' }
1717promise2 . then ( console . log ) ; // 'value2'
1818
@@ -48,14 +48,14 @@ const readFile = (filename, encoding) =>
4848 } ) ) ;
4949
5050readFile ( 'file1.txt' , 'utf8' )
51- . then ( data => {
51+ . then ( ( data ) => {
5252 console . log ( data ) ;
5353 return readFile ( 'file2.txt' , 'utf8' ) ;
5454 } )
55- . then ( data => {
55+ . then ( ( data ) => {
5656 console . log ( data ) ;
5757 return readFile ( 'file3.txt' , 'utf8' ) ;
5858 } )
59- . then ( data => {
59+ . then ( ( data ) => {
6060 console . log ( data ) ;
6161 } ) ;
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- const promisify = fn => ( ...args ) => new Promise ( ( resolve , reject ) => {
3+ const promisify = ( fn ) => ( ...args ) => new Promise ( ( resolve , reject ) => {
44 args . push ( ( err , result ) => {
55 if ( err ) reject ( err ) ;
66 else resolve ( result ) ;
@@ -13,18 +13,18 @@ const fs = require('fs');
1313const readFile1 = promisify ( fs . readFile ) ;
1414
1515readFile1 ( 'file1.txt' , 'utf8' )
16- . then ( data => {
16+ . then ( ( data ) => {
1717 console . log ( data . toString ( ) ) ;
1818 return readFile1 ( 'file2.txt' , 'utf8' ) ;
1919 } )
20- . then ( data => {
20+ . then ( ( data ) => {
2121 console . log ( data . toString ( ) ) ;
2222 return readFile1 ( 'file3.txt' , 'utf8' ) ;
2323 } )
24- . then ( data => {
24+ . then ( ( data ) => {
2525 console . log ( data . toString ( ) ) ;
2626 } )
27- . catch ( err => {
27+ . catch ( ( err ) => {
2828 console . log ( err ) ;
2929 } ) ;
3030
@@ -33,17 +33,17 @@ const util = require('util');
3333const readFile2 = util . promisify ( fs . readFile ) ;
3434
3535readFile2 ( 'file1.txt' , 'utf8' )
36- . then ( data => {
36+ . then ( ( data ) => {
3737 console . log ( data . toString ( ) ) ;
3838 return readFile2 ( 'file2.txt' , 'utf8' ) ;
3939 } )
40- . then ( data => {
40+ . then ( ( data ) => {
4141 console . log ( data . toString ( ) ) ;
4242 return readFile2 ( 'file3.txt' , 'utf8' ) ;
4343 } )
44- . then ( data => {
44+ . then ( ( data ) => {
4545 console . log ( data . toString ( ) ) ;
4646 } )
47- . catch ( err => {
47+ . catch ( ( err ) => {
4848 console . log ( err ) ;
4949 } ) ;
Original file line number Diff line number Diff line change 22
33const fs = require ( 'fs' ) ;
44
5- const readTextFile = filename => fs . promises . readFile ( filename , 'utf8' ) ;
5+ const readTextFile = ( filename ) => fs . promises . readFile ( filename , 'utf8' ) ;
66
77readTextFile ( 'file1-.txt' )
88 . then (
9- data => {
9+ ( data ) => {
1010 console . dir ( { file1 : data } ) ;
1111 return readTextFile ( 'file2.txt' ) ;
1212 } ,
13- reason => {
13+ ( reason ) => {
1414 console . log ( 'Cannot read file1.txt --- A' ) ;
1515 console . log ( reason ) ;
1616 }
1717 )
1818 . catch (
19- reason => {
19+ ( reason ) => {
2020 console . log ( 'Cannot read file1.txt --- B' ) ;
2121 console . log ( reason ) ;
2222 }
2323 )
24- . then ( data => {
24+ . then ( ( data ) => {
2525 console . dir ( { file2 : data } ) ;
2626 return readTextFile ( 'file3.txt' ) ;
2727 } )
28- . catch ( reason => {
28+ . catch ( ( reason ) => {
2929 console . log ( 'Cannot read file2.txt' ) ;
3030 console . log ( reason ) ;
3131 } )
32- . then ( data => {
32+ . then ( ( data ) => {
3333 console . dir ( { file3 : data } ) ;
3434 } )
35- . catch ( reason => {
35+ . catch ( ( reason ) => {
3636 console . log ( 'Cannot read file' ) ;
3737 console . log ( reason ) ;
3838 } ) ;
Original file line number Diff line number Diff line change 22
33const fs = require ( 'fs' ) ;
44
5- const readTextFile = filename => fs . promises . readFile ( filename , 'utf8' ) ;
5+ const readTextFile = ( filename ) => fs . promises . readFile ( filename , 'utf8' ) ;
66
77readTextFile ( 'file1-.txt' )
8- . then ( data => {
8+ . then ( ( data ) => {
99 console . dir ( { file1 : data } ) ;
1010 } )
11- . catch ( reason => {
11+ . catch ( ( reason ) => {
1212 console . log ( 'Cannot read file1.txt' ) ;
1313 console . log ( reason ) ;
1414 } )
Original file line number Diff line number Diff line change 22
33const http = require ( 'http' ) ;
44
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 ) => {
77 const code = res . statusCode ;
88 if ( code !== 200 ) {
99 return reject ( new Error ( `HTTP status code ${ code } ` ) ) ;
@@ -12,7 +12,7 @@ const fetch = url => new Promise((resolve, reject) => {
1212 res . on ( 'error' , reject ) ;
1313
1414 const chunks = [ ] ;
15- res . on ( 'data' , chunk => {
15+ res . on ( 'data' , ( chunk ) => {
1616 chunks . push ( chunk ) ;
1717 } ) ;
1818
Original file line number Diff line number Diff line change 33const fetch = require ( './6-fetch.js' ) ;
44
55fetch ( 'http://localhost:3000/person' )
6- . then ( data => {
6+ . then ( ( data ) => {
77 console . log ( data ) ;
88 } )
9- . catch ( err => {
9+ . catch ( ( err ) => {
1010 console . error ( err ) ;
1111 } ) ;
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ const promises = [
1111] ;
1212
1313Promise . all ( promises )
14- . then ( values => {
14+ . then ( ( values ) => {
1515 console . log ( values ) ;
1616 } )
17- . catch ( err => {
17+ . catch ( ( err ) => {
1818 console . log ( err ) ;
1919 } ) ;
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ const promises = [
1111] ;
1212
1313Promise . race ( promises )
14- . then ( res => {
14+ . then ( ( res ) => {
1515 console . log ( res ) ;
1616 } )
17- . catch ( err => {
17+ . catch ( ( err ) => {
1818 console . log ( err ) ;
1919 } ) ;
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class Thenable {
1919 if ( fn ) {
2020 const next = fn ( value ) ;
2121 if ( next ) {
22- next . then ( value => {
22+ next . then ( ( value ) => {
2323 this . next . resolve ( value ) ;
2424 } ) ;
2525 }
@@ -29,7 +29,7 @@ class Thenable {
2929
3030// Usage
3131
32- const readFile = filename => {
32+ const readFile = ( filename ) => {
3333 const thenable = new Thenable ( ) ;
3434 fs . readFile ( filename , 'utf8' , ( err , data ) => {
3535 if ( err ) throw err ;
@@ -39,14 +39,14 @@ const readFile = filename => {
3939} ;
4040
4141readFile ( 'file1.txt' )
42- . then ( data => {
42+ . then ( ( data ) => {
4343 console . dir ( { file1 : data } ) ;
4444 return readFile ( 'file2.txt' ) ;
4545 } )
46- . then ( data => {
46+ . then ( ( data ) => {
4747 console . dir ( { file2 : data } ) ;
4848 return readFile ( 'file3.txt' ) ;
4949 } )
50- . then ( data => {
50+ . then ( ( data ) => {
5151 console . dir ( { file3 : data } ) ;
5252 } ) ;
Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ const PROMISE_TIMEOUT = 1000;
55class Expirable {
66 constructor ( executor ) {
77 const promise = new Promise ( ( resolve , reject ) => {
8- executor ( val => {
8+ executor ( ( val ) => {
99 if ( this . expired ) return ;
1010 clearTimeout ( this . timer ) ;
1111 resolve ( val ) ;
12- } , err => {
12+ } , ( err ) => {
1313 if ( this . expired ) return ;
1414 clearTimeout ( this . timer ) ;
1515 reject ( err ) ;
@@ -28,42 +28,42 @@ class Expirable {
2828
2929// Usage
3030
31- new Expirable ( resolve => {
31+ new Expirable ( ( resolve ) => {
3232 setTimeout ( ( ) => {
3333 resolve ( 'Resolved before timeout' ) ;
3434 } , 100 ) ;
35- } ) . then ( data => {
35+ } ) . then ( ( data ) => {
3636 console . dir ( { data } ) ;
37- } ) . catch ( error => {
37+ } ) . catch ( ( error ) => {
3838 console . dir ( { error : error . message } ) ;
3939} ) ;
4040
4141new Expirable ( ( resolve , reject ) => {
4242 setTimeout ( ( ) => {
4343 reject ( new Error ( 'Something went wrong' ) ) ;
4444 } , 100 ) ;
45- } ) . then ( data => {
45+ } ) . then ( ( data ) => {
4646 console . dir ( { data } ) ;
47- } ) . catch ( error => {
47+ } ) . catch ( ( error ) => {
4848 console . dir ( { error : error . message } ) ;
4949} ) ;
5050
51- new Expirable ( resolve => {
51+ new Expirable ( ( resolve ) => {
5252 setTimeout ( ( ) => {
5353 resolve ( 'Never resolved before timeout' ) ;
5454 } , 2000 ) ;
55- } ) . then ( data => {
55+ } ) . then ( ( data ) => {
5656 console . dir ( { data } ) ;
57- } ) . catch ( error => {
57+ } ) . catch ( ( error ) => {
5858 console . dir ( { error : error . message } ) ;
5959} ) ;
6060
6161new Expirable ( ( resolve , reject ) => {
6262 setTimeout ( ( ) => {
6363 reject ( new Error ( 'Never rejected before timeout' ) ) ;
6464 } , 2000 ) ;
65- } ) . then ( data => {
65+ } ) . then ( ( data ) => {
6666 console . dir ( { data } ) ;
67- } ) . catch ( error => {
67+ } ) . catch ( ( error ) => {
6868 console . dir ( { error : error . message } ) ;
6969} ) ;
You can’t perform that action at this time.
0 commit comments