I have written a function that returns a promise to read a file line by line asynchronously but the problem is that if the file doesn't exist an error is thrown and the application crashes. I have surrounded the code in a try catch block but the code execution never goes into the catch block but terminates.
Secondly can I reject a promise and then return a json object from the promise?
Like reject({success: false});
const fs = require('fs');
const readline = require('readline');
class fileParser{
readPolicyFile = function(filePath, fileName) {
return new Promise((resolve, reject) => {
try {
let response = {};
let readInterface = readline.createInterface({
input: fs.createReadStream(`${filePath}/${fileName}`),
terminal: false
});
readInterface
.on('line', line => {
// Do processing here to create the response
})
.on('close', () => {
resolve(response);
});
} catch(err){
console.log("Inside catach block");
reject(err);
}
})
}
}
module.exports = new fileParser();