I run script on server:
const fs = require('fs');
async function loadData() {
fs.readFile('ADDR To FILE\\cfg.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
map = JSON.parse(data);
console.log("1:" + serverAddr);
serverAddr = map.serverAddr;
console.log("2:" + serverAddr);
});
console.log("3:" + serverAddr);
console.log("4:" + serverAddr);
}
var serverAddr = "NOT INIT";
console.log("5:" + serverAddr);
loadData();
console.log("6:" + serverAddr);
cfg.json
file (from same folder):
{
"serverAddr":"https://google.com/"
}
And my output is:
5:NOT INIT
3:NOT INIT
4:NOT INIT
6:NOT INIT
1:NOT INIT
2:https://google.com/
What is wrong? If I add await
before fs.readFile
I receive message form VS Code: 'await' has no effect on the type of this expression.ts(80007)
At the beginning of my program, I need to initialize global variables using .json
file from hard disc. What is wrong? Why it is not executed in order? I expect execution 1 and 2 before 3 and 4, but with ant without await
, console.log() 1 and 2 are at the end of program.