In the code below the function, readFileAsync() is used to read a file and return a new Promise. It doesn't throw an error but it may not be structured properly.
function readFileAsync(filename) {
return new Promise( (resolve, reject) => {
fs.readFile((path.join(__dirname, filename)), { encoding: 'utf-8' }, (err, data) => {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
console.log(readFileAsync('/helperData/jsonFormat.txt'));
Question: How do I structure this example to use .then()
and .catch()
? No matter where I locate these (inside or outside the function) it throws an exception. I feel like I might have coded myself into a corner and I'm not sure how this pattern needs to be laid out.