Q:

Having an issue with callback function

So I have some code like this 

const getAPIData = (symbol, callback) => {
  var options = {
    url: "https://api.binance.com/api/v3/ticker/price",
    method: "GET",
    qs: {
      symbol
    },
  };
  request(options, (err, res, body) => {
    body = JSON.parse(body);
    callback(body);
  });
};

var isValid = 0;
getAPIData(symbol, (body) => {
  console.log(body);
  if (body.symbol) {
    console.log("yes");
    isValid = 1;
  } else {
    console.log("no");
  }
});

After this callback is executed the "isValid" variable still remains 0 no matter what the outcome is. Although the console gets logged with yes and no both. The isValid variable still remains 0 when I debug the program.
How can the console.log function work and not set the isValid to 1? it's like it's just skipping that line or I'm not sure. Please help me out!

A:

This is the way asynchronous calls work.
  var isValid = 0;
  getAPIData(symbol, (body) => {
    console.log(body);
    if (body.symbol) {
      console.log("yes");
      isValid = 1;
      console.log(isValid); // 1
    } else {
      console.log("no");
    }
  });

console.log(isValid); // 0
// when the JS engine gets here, isValid will still be 0
// since getAPIData is asynchronous and it's still in progress at this point
// also, you cannot use any results of getAPIData here

