I am trying to store JSON file content into a Javascript array, but I am getting an empty array as result.
const displayTask = () => {
let text = [];
fs.readFile(fileConfig, (err, data) => {
if (err) throw err;
const content = JSON.parse(data);
for (let task in content) {
text.push(task);
}
});
console.log(text);
};
output -> []
Thanks to everyone, found the correct solution:
const displayTask = async() => {
return await new Promise((resolve, reject) => {
fs.readFile(fileConfig, (err, data) => {
if(err) throw err;
text = JSON.parse(data);
console.log(text);
resolve();
})
})
};