I am writing a node js program to read content from a file , I am executing this program multiple times in a short time. Sometimes I can see file content and sometimes I do not (PFA screenshot) , can anyone please explain why this is happening ? Is this because I am not using promises ?
var fs= require('fs');
fs.readFile('myData.json', (err, data) => {
if(err)
console.log('Error Found',err.code);
else {
console.log('inside else');
fs.open('myData.json', 'wx', (err, fd) => {
console.log('inside open file');
fs.writeFile('myData.json','test data',(err)=>{
if(err) console.log('error writing data');
});
fs.readFile('myData.json','utf8',(err, data) => {
console.log('read file'+data);
});
});
}
});
Screen Shot :
