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
33const fs = require ( 'fs' ) ;
44
5- function readFile ( filename ) {
6- return new Promise ( ( resolve , reject ) => {
7- fs . readFile ( filename , ( err , data ) => {
8- if ( err ) {
9- reject ( err ) ;
10- } else {
11- resolve ( data . toString ( ) ) ;
12- }
13- } ) ;
5+ const readFile = ( filename ) => new Promise ( ( resolve , reject ) => {
6+ fs . readFile ( filename , ( err , data ) => {
7+ if ( err ) reject ( err ) ;
8+ else resolve ( data . toString ( ) ) ;
149 } ) ;
15- }
16-
17- readFile ( 'file1.txt' ) . then ( ( data ) => {
18- console . log ( data ) ;
19- return readFile ( 'file2.txt' ) ;
20- } ) . then ( ( data ) => {
21- console . log ( data ) ;
22- return readFile ( 'file3.txt' ) ;
23- } ) . then ( ( data ) => {
24- console . log ( data ) ;
2510} ) ;
11+
12+ readFile ( 'file1.txt' )
13+ . then ( data => {
14+ console . log ( data ) ;
15+ return readFile ( 'file2.txt' ) ;
16+ } )
17+ . then ( data => {
18+ console . log ( data ) ;
19+ return readFile ( 'file3.txt' ) ;
20+ } )
21+ . then ( data => {
22+ console . log ( data ) ;
23+ } ) ;
Original file line number Diff line number Diff line change @@ -5,12 +5,15 @@ const promisify = require('./promisify');
55
66const readFile = promisify ( fs . readFile ) ;
77
8- readFile ( 'file1.txt' ) . then ( ( data ) => {
9- console . log ( data . toString ( ) ) ;
10- return readFile ( 'file2.txt' ) ;
11- } ) . then ( ( data ) => {
12- console . log ( data . toString ( ) ) ;
13- return readFile ( 'file3.txt' ) ;
14- } ) . then ( ( data ) => {
15- console . log ( data . toString ( ) ) ;
16- } ) ;
8+ readFile ( 'file1.txt' )
9+ . then ( data => {
10+ console . log ( data . toString ( ) ) ;
11+ return readFile ( 'file2.txt' ) ;
12+ } )
13+ . then ( data => {
14+ console . log ( data . toString ( ) ) ;
15+ return readFile ( 'file3.txt' ) ;
16+ } )
17+ . then ( data => {
18+ console . log ( data . toString ( ) ) ;
19+ } ) ;
Original file line number Diff line number Diff line change @@ -5,17 +5,18 @@ const promisify = require('./promisify');
55
66const readFile = promisify ( fs . readFile ) ;
77
8- function readTextFile ( filename ) {
9- const promise = readFile ( filename ) . then ( data => data . toString ( ) ) ;
10- return promise ;
11- }
8+ const readTextFile = filename => readFile ( filename )
9+ . then ( data => data . toString ( ) ) ;
1210
13- readTextFile ( 'file1.txt' ) . then ( ( data ) => {
14- console . log ( data ) ;
15- return readTextFile ( 'file2.txt' ) ;
16- } ) . then ( ( data ) => {
17- console . log ( data ) ;
18- return readTextFile ( 'file3.txt' ) ;
19- } ) . then ( ( data ) => {
20- console . log ( data ) ;
21- } ) ;
11+ readTextFile ( 'file1.txt' )
12+ . then ( ( data ) => {
13+ console . log ( data ) ;
14+ return readTextFile ( 'file2.txt' ) ;
15+ } )
16+ . then ( ( data ) => {
17+ console . log ( data ) ;
18+ return readTextFile ( 'file3.txt' ) ;
19+ } )
20+ . then ( ( data ) => {
21+ console . log ( data ) ;
22+ } ) ;
Original file line number Diff line number Diff line change @@ -5,20 +5,22 @@ const promisify = require('./promisify');
55
66const readFile = promisify ( fs . readFile ) ;
77
8- function readTextFile ( filename ) {
9- const promise = readFile ( filename ) . then ( data => data . toString ( ) ) ;
10- return promise ;
11- }
8+ const readTextFile = filename => readFile ( filename )
9+ . then ( data => data . toString ( ) ) ;
1210
13- readTextFile ( 'file1.txt' ) . then ( ( data ) => {
14- console . log ( data ) ;
15- return readTextFile ( 'file2.txt' ) ;
16- } ) . then ( ( data ) => {
17- console . log ( data ) ;
18- return readTextFile ( 'file3-!!!.txt' ) ;
19- } ) . then ( ( data ) => {
20- console . log ( data ) ;
21- } ) . catch ( ( reason ) => {
22- console . log ( 'Cannot read file' ) ;
23- console . log ( reason ) ;
24- } ) ;
11+ readTextFile ( 'file1.txt' )
12+ . then ( ( data ) => {
13+ console . log ( data ) ;
14+ return readTextFile ( 'file2.txt' ) ;
15+ } )
16+ . then ( ( data ) => {
17+ console . log ( data ) ;
18+ return readTextFile ( 'file3-!!!.txt' ) ;
19+ } )
20+ . then ( ( data ) => {
21+ console . log ( data ) ;
22+ } )
23+ . catch ( ( reason ) => {
24+ console . log ( 'Cannot read file' ) ;
25+ console . log ( reason ) ;
26+ } ) ;
Original file line number Diff line number Diff line change @@ -5,21 +5,22 @@ const promisify = require('./promisify');
55
66const readFile = promisify ( fs . readFile ) ;
77
8- function readTextFile ( filename ) {
9- const promise = readFile ( filename ) . then ( data => data . toString ( ) ) ;
10- return promise ;
11- }
8+ const readTextFile = filename => readFile ( filename )
9+ . then ( data => data . toString ( ) ) ;
1210
13- readTextFile ( 'file1.txt' ) . then ( ( data ) => {
14- console . log ( data ) ;
15- return readTextFile ( 'file2-!!!.txt' ) ;
16- } ) . then ( ( data ) => {
17- console . log ( data ) ;
18- return readTextFile ( 'file3.txt' ) ;
19- } , ( reason ) => {
20- console . log ( 'Cannot read file' ) ;
21- console . log ( reason ) ;
22- return readTextFile ( 'file3.txt' ) ;
23- } ) . then ( ( data ) => {
24- console . log ( data ) ;
25- } ) ;
11+ readTextFile ( 'file1.txt' )
12+ . then ( ( data ) => {
13+ console . log ( data ) ;
14+ return readTextFile ( 'file2-!!!.txt' ) ;
15+ } )
16+ . then ( ( data ) => {
17+ console . log ( data ) ;
18+ return readTextFile ( 'file3.txt' ) ;
19+ } , ( reason ) => {
20+ console . log ( 'Cannot read file' ) ;
21+ console . log ( reason ) ;
22+ return readTextFile ( 'file3.txt' ) ;
23+ } )
24+ . then ( ( data ) => {
25+ console . log ( data ) ;
26+ } ) ;
Original file line number Diff line number Diff line change 33const httpGet = require ( './get-json' ) ;
44
55const baseUrl = 'http://localhost:3000/' ;
6- httpGet ( baseUrl + 'person' ) . then ( ( data ) => {
7- console . log ( data ) ;
8- return httpGet ( baseUrl + 'city' ) ;
9- } ) . then ( console . log ) ;
6+
7+ httpGet ( baseUrl + 'person' )
8+ . then ( ( data ) => {
9+ console . log ( data ) ;
10+ return httpGet ( baseUrl + 'city' ) ;
11+ } )
12+ . then ( console . log ) ;
Original file line number Diff line number Diff line change 33const httpGet = require ( './get-json' ) ;
44
55const baseUrl = 'http://localhost:3000/' ;
6+
67httpGet ( baseUrl ) . then ( ( api ) => {
78 const promises = [ ] ;
89 for ( const resource of api . resources ) {
Original file line number Diff line number Diff line change 22
33const http = require ( 'http' ) ;
44
5- module . exports = ( url ) => {
6- return new Promise ( ( resolve , reject ) => {
7- http . get ( url , res => {
8- const code = res . statusCode ;
9- if ( code !== 200 ) {
10- return reject ( new Error ( `HTTP status code ${ code } ` ) ) ;
11- }
5+ module . exports = ( url ) => new Promise ( ( resolve , reject ) => {
6+ http . get ( url , res => {
7+ const code = res . statusCode ;
8+ if ( code !== 200 ) {
9+ return reject ( new Error ( `HTTP status code ${ code } ` ) ) ;
10+ }
1211
13- res . on ( 'error' , reject ) ;
12+ res . on ( 'error' , reject ) ;
1413
15- const chunks = [ ] ;
16- res . on ( 'data' , chunk => {
17- chunks . push ( chunk ) ;
18- } ) ;
14+ const chunks = [ ] ;
15+ res . on ( 'data' , chunk => {
16+ chunks . push ( chunk ) ;
17+ } ) ;
1918
20- res . on ( 'end' , ( ) => {
21- const json = Buffer . concat ( chunks ) . toString ( ) ;
22- let object = null ;
19+ res . on ( 'end' , ( ) => {
20+ const json = Buffer . concat ( chunks ) . toString ( ) ;
21+ let object = null ;
2322
24- try {
25- object = JSON . parse ( json ) ;
26- } catch ( error ) {
27- return reject ( error ) ;
28- }
23+ try {
24+ object = JSON . parse ( json ) ;
25+ } catch ( error ) {
26+ return reject ( error ) ;
27+ }
2928
30- resolve ( object ) ;
31- } ) ;
29+ resolve ( object ) ;
3230 } ) ;
3331 } ) ;
34- } ;
32+ } ) ;
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- module . exports = function promisify ( asyncFunction ) {
4- return ( ...args ) => {
5- return new Promise ( ( resolve , reject ) => {
6- args . push ( ( err , result ) => {
7- if ( err ) {
8- reject ( err ) ;
9- } else {
10- resolve ( result ) ;
11- }
12- } ) ;
13- asyncFunction ( ...args ) ;
3+ module . exports = asyncFunction => ( ...args ) => (
4+ new Promise ( ( resolve , reject ) => {
5+ args . push ( ( err , result ) => {
6+ if ( err ) reject ( err ) ;
7+ else resolve ( result ) ;
148 } ) ;
15- } ;
16- } ;
9+ asyncFunction ( ...args ) ;
10+ } )
11+ ) ;
You can’t perform that action at this time.
0 commit comments