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);
}
});
try to change this:
to :
because
once(myDB)
isasync
and returns apromise
you can put the callback in thepromise.then(v=>/*your callback*/)