This code is not giving output as I want.
const fs = require('fs');
const rl = require("readline");
async function readTwoColumnFile() {
console.log('reading file');
// (C) READ LINE-BY-LINE INTO ARRAY
const reader = rl.createInterface({
input: fs.createReadStream("index.js")
});
reader.on("line", (row) => {
//some code
});
// (D) DONE - FULL ARRAY
reader.on("close", async () => {
// some code
console.log('reading complete')
res = 'Hello World!'
return res
});
}
async function run(){
const res = await readTwoColumnFile()
console.log('data' , res)
}
run()
Here the line console.log('data', res)
is executing without res
being initialized so when I run this code my output is coming
reading file
data undefined
reading complete
Instead of
reading file
reading complete
data Hello World!
So how can I wait for res
to get executed after initilazation?