0

I am trying to set a global variable within a function, but the code continues before the variable has been updated. e.g.

        var username = 'Example';
 const fetch = require('node-fetch');
 var num = 1234;
 var uuidP;
 const request = async () => {
  const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`);
  const json = await response.json();
  uuidP = json.id;
 }
 request();

 console.log(num); //returns 1234
 console.log(uuidP); //returns udefined
Edaz
  • 300
  • 1
  • 6
  • 16

1 Answers1

1

Javascript is heavily optimised. You need to declare the update() function is asynchronous, and then use a Promise to await the response of the update. Have a look at this example.

Paul
  • 32
  • 5