Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
Bash script calls a .js file with a function that should update global variable, but it doesn't

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 runs updateGlobalVar.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 new 222 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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda