This is my sample code:
var dicWords = []
function ReadFile()
{
var fs = require('fs')
fs.readFile('words_alpha_sorted.txt', (err, data) =>{
if (err){
console.log(err);
return;
}
data = String(data)
dicWords = data.split(/\r?\n/);
})
}
function DoStuffWithFile();
{
//do stuff with the variable dicWords
}
function main()
{
ReadFile();
DoStuffWithFile();
}
How can I can I read the file, wait for it to be completely read, then the rest of the code being executed, so that dicWords isn't an empty array when DoStuffWithFile is called? Using JS (Node). I want to call DoStuffWithFile from the main function, not ReadFile. Note-: The main function is not actually the main function, but it's where the file management is happening.