I have the following code that checks the existance of a file and if present, parses the JSON data from it:
fs.access(dbPath, (err) => {
if (err) throw err
else{
console.log('Database found. Processing data.');
dbObj = processDatabaseFile(dbPath);
console.log('checkonstartup: '+dbObj);
return;
}
});
And here is the processDatabaseFile:
function processDatabaseFile(path){
fs.readFile(path, (err, data) =>{
if (err) throw err;
let jsonData = JSON.parse(data);
return jsonData;
})
}
My problem is when the fs.access function is being called, it returns sooner than the processDatabaseFile(dbPath)
returns.
Eg. I want to make it use async await, but I don't know how to do that.
Tried putting async (err) => { ...
declaration and then using
dbObj = await processDatabaseFile(dbPath)
but it gives errors around the if-else and semicolons and missing brackets.
Feedback is appreciated.
EDIT: About the errors regarding adding async-await. I was using VSCode and it turns out ESLint and JSHint conflicts each other.