Skip to content

Commit c675f50

Browse files
committed
Improve I/O examples
1 parent 0d3b4e6 commit c675f50

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

JavaScript/4-catch.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
'use strict';
22

3-
const fs = require('node:fs');
3+
const { readFile } = require('node:fs/promises');
44

5-
const readTextFile = (filename) => (
6-
fs.promises.readFile(filename, 'utf8')
7-
);
8-
9-
readTextFile('file1.txt')
5+
readFile('file1.txt', 'utf8')
106
.then((data) => {
117
console.dir({ file1: data });
12-
return readTextFile('file2.txt');
8+
return readFile('file2.txt', 'utf8');
139
}, (reason) => {
14-
console.log('Cannot read file1.txt --- A');
10+
console.log('1: Cannot read file1.txt');
1511
console.log(reason);
1612
})
1713
.catch((reason) => {
18-
console.log('Cannot read file1.txt --- B');
14+
console.log('2: Cannot read file1.txt');
1915
console.log(reason);
2016
})
2117
.then((data) => {
2218
console.dir({ file2: data });
23-
return readTextFile('file3.txt');
19+
return readFile('file3.txt', 'utf8');
2420
})
2521
.catch((reason) => {
2622
console.log('Cannot read file2.txt');

JavaScript/5-finally.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const fs = require('node:fs');
3+
const { readFile } = require('node:fs/promises');
44

5-
fs.promises.readFile('file1.txt', 'utf8')
5+
readFile('file1.txt', 'utf8')
66
.then((data) => {
77
console.dir({ file1: data });
88
})

0 commit comments

Comments
 (0)