-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-promises.js
More file actions
34 lines (30 loc) · 1.12 KB
/
Copy path7-promises.js
File metadata and controls
34 lines (30 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const {readFile, writeFile} = require('fs');
const util = require('util');
const readFilePromise = util.promisify(readFile);
const writeFilePromise = util.promisify(writeFile);
// const getText = (path) => {
// return new Promise((resolve, reject) => {
// readFile(path, 'utf8', (err, data) => {
// if (err) {
// reject(err);
// } else {
// resolve(data);
// }
// });
// });
// }
const start = async () => {
try {
// const text = await getText('./testFolder/first.txt');
// const text2 = await getText('./testFolder/second.txt');
const text1_1 = await readFilePromise('./testFolder/first.txt', 'utf8');
const text1_2 = await readFilePromise('./testFolder/second.txt', 'utf8');
await writeFilePromise('./testFolder/new.txt', 'This is new with promisify');
// console.log(text, text2);
console.log(text1_1, text1_2);
} catch (error) {
console.log(error);
}
}
start();
// getText('./testFolder/first.txt').then((result)=>console.log(result)).catch((err)=>console.log(err));