-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathassignment3Solution.js
More file actions
27 lines (24 loc) · 948 Bytes
/
assignment3Solution.js
File metadata and controls
27 lines (24 loc) · 948 Bytes
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
// Create promisified version of fs.readFile
// import the fs module
const fs = require('fs');
// function to create a promisified version of fs.readFile
function promisifiedReadFile(path) {
// return a promise that resolves after the file is read
return new Promise((resolve, reject) => {
// read the file from the given path
fs.readFile(path, 'utf8', (error, data) => {
// if the file is read successfully, resolve the promise with the data
if (!error) {
resolve(data);
} else {
// if the file read fails, reject the promise with the error
reject(error);
}
});
}
);
}
// call the promisifiedReadFile function with the path of the file to read
promisifiedReadFile('a.txt')
.then((data) => console.log(data))
.catch((error) => console.log(error));