Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda