File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const fs = require ( 'fs' ) ;
4+
5+ const readTextFile = filename => fs . promises . readFile ( filename , 'utf8' ) ;
6+
7+ readTextFile ( 'file1-.txt' )
8+ . then ( data => {
9+ console . dir ( { file1 : data } ) ;
10+ } )
11+ . catch ( reason => {
12+ console . log ( 'Cannot read file1.txt' ) ;
13+ console . log ( reason ) ;
14+ } )
15+ . finally ( ( ) => {
16+ console . log ( 'finally' ) ;
17+ } ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const http = require ( 'http' ) ;
4+
5+ const fetch = 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+ }
11+
12+ res . on ( 'error' , reject ) ;
13+
14+ const chunks = [ ] ;
15+ res . on ( 'data' , chunk => {
16+ chunks . push ( chunk ) ;
17+ } ) ;
18+
19+ res . on ( 'end' , ( ) => {
20+ const json = Buffer . concat ( chunks ) . toString ( ) ;
21+ try {
22+ const object = JSON . parse ( json ) ;
23+ resolve ( object ) ;
24+ } catch ( error ) {
25+ return reject ( error ) ;
26+ }
27+ } ) ;
28+ } ) ;
29+ } ) ;
30+
31+ module . exports = fetch ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments