0

please dont close this as its not the same as an undefined var that you think this is the same question as its not.

Trying to change a global var from inside an async function then read it from outside the function, I will pop example on oh and i'm new to javascript, now if I read the value from inside the function its fine but not from outside. EDIT forgot to say its webaccess the var I'm wanting to use.

var webaccess = (0);
async function run() {
var result = (0);
  
  result = await contract.methods.name().call()
  console.log(`Token name: ${result}`)

  result = await contract.methods.balanceOf(account1).call()
  console.log(`Balance of account #1: ${result}`)

   if (result >= "10000000000000000000" && result < "12000000000000000000"){
   webaccess = (1);
   }
   if (result >= "12000000000000000000" && result < "14000000000000000000"){
   webaccess = (2);
   }
   if (result >= "14000000000000000000" && result < "16000000000000000000"){
   webaccess = (3);
   }
   if (result >= "16000000000000000000" && result < "18000000000000000000"){
   webaccess = (4);
   }
   if (result >= "18000000000000000000" && result < "20000000000000000000"){
   webaccess = (5);
   }
}
   
   run()

    if (webaccess == (1)){
      console.log(`webaccess pass = ${webaccess}`);
    }
    if (webaccess == (2)){
      console.log(`webaccess pass = ${webaccess}`);
    }
    if (webaccess == (3)){
      console.log(`webaccess pass = ${webaccess}`);
    }
    if (webaccess == (4)){
      console.log(`webaccess pass = ${webaccess}`);
    }
    if (webaccess == (5)){
      console.log(`webaccess pass = ${webaccess}`);
    } 
  • Asynchronicity means that logic can happen in (seemingly) parallel. Since `run()` is designed as an asynchronous method, there is no guarantee that it will have finished running before the logic after it tries to execute. – Taplar Jul 08 '21 at 22:57
  • https://stackoverflow.com/users/1586174/taplar was it you who closed my question its not the same as an underined var as you linked to here This question already has answers here: Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference (6 answers) i clearly difened mine – Retro Phil Jul 08 '21 at 23:04
  • The notice at the top of the thread says who closed the thread as a duplicate. – Taplar Jul 13 '21 at 22:34

0 Answers0