-1

I define in Node.js a global variable and give it a value. A variable with the same name (from my point of view the global variable) is changed in a sub function. However, the value of the global variable remains the same.

Re-named the variable

global.reo = "HTMLcode{${table}}HTMLcode"; //global variable "reo" defined, as html code is translated in this page I just put "HTMLcode" instead or real HTML code

...

function setResHtml(sql, cb){
   ...
   return cb(table);
});


let sql ='SELECT * FROM table';
setResHtml(sql, resql => {

      reo = reo.replace('{${table}}', resql); // changed global value "reo"
      console.log(reo); // shows the change

  });

    console.log(reo); //doesn't show the change

1 Answers1

0

The last console.log() executes first because the first one in is in an asynchronous callback function (it executes later). So you are outputting that value before it is changed... that's why the change doesn't show.

Alex
  • 41
  • 1
  • Did not realize that node.js often asynchronously executes the functions. I could solve the problem by restructuring my code. Thanks for every comment! – user11300277 Apr 09 '19 at 21:37