I'm trying to set value in a global variable result but it's returning undefined
Please find my code below:
myFile.js
const fs = require('fs');
var result
async function processFile(fileName) {
fs.readFile(fileName, (err, data) => {
if (err) throw err;
let student = JSON.parse(data);
//console.log(student);
result = student;
});
}
processFile('src/file.json')
console.log('Get Results')
console.log(result)
file.json
{
"name": "Sara",
"age": 23,
"gender": "Female",
"department": "History",
"car": "Honda"
}
I'm not sure why this one result = student; is not setting in a global variable. Could you please help me with this? I would like access result variables in multiple places.
Thanks