I have a updateGlobalVar.js
file which looks as following:
var globalVar = 111;
async function setGlobalVar() {
globalVar = 222;
}
setGlobalVar();
Also, I have a bash script, which retrieves that globalVar
value, and looks as following:
#!/bin/bash
npm run /updateGlobalVar.js
GLOBAL_VAR=$(grep -Po '(?<=^var globalVar = ).*?(?=;)' ./updateGlobalVar.js)
echo $GLOBAL_VAR
The problem here is the bash script prints out initial 111
instead of an updated 222
.
The way I understand that flow is:
npm run /updateGlobalVar.js
runsupdateGlobalVar.js
(and I am sure it does);- function
setGlobalVar()
is called (and I am sure it is); globalVar
should be now updated and printed with a new222
value. But it doesn’t.
Moreover, when I modify my file to “debug” using the console.log()
within the updateGlobalVar.js
file like:
var globalVar = 111;
console.log(globalVar);
async function setGlobalVar() {
globalVar = 222;
}
setGlobalVar();
console.log(globalVar);
It returns 111
in both console.log(globalVar)
cases, which makes me think that globalVar
is never updated.
Looks like I miss something fundamental regarding the work with global variables, still am struggling to figure that out.
May this be somehow related to the asynchronous nature of the setGlobalVar()
function?