I have below global variable and trying to update in diffrent methods
var companyName;
In this method i am getting company name and can able to print the companyName in terminal
var once = async function(myDB) {
if(once.done) return;
companyName = await myCacheData.defaultCompany();
console.log('Company inside once :' + companyName);
};
In this method i am trying to get updated company name, which one is updated in side once() function. but when i tried to print in terminal it is showing undefined.
db.dbConnect(function (err) {
if (err) {
process.exit(1)
}
else {
myDB = db.getDBConnection();
app.myDB = myDB;
once(myDB);
console.log('Company inside db call :' + companyName);
}
});
once.myCacheData.defaultCompany();may be returning nothingdb.getDBConnection()is alsoasyncand needs to be awaited as well. But in the context of what you told us , then what @RolandStarke already saidasyncfunction don't block. They return a promise. If you want to wait for their result to be done, you have to use.then()on that promise.