I am trying to read a file using fs.readFile() and then use the result in the code that follows. The read process has to be asynchronous. Here is what I have so far:
var file = async () => {
await fs.readFile(__dirname + "/file.json", "utf-8", (err, data) => {
if (err) throw err
return JSON.parse(data)
})
}
console.log(file());
For some reason this code always logs undefined. Is there any way to do this without moving the console.log statement inside the readFile callback?