-1

I have this code and I want to use the coinPrice variable outside the function, but I can't do it. Can anyone help me out ?

var burl = 'https://fapi.binance.com';
var query = '/fapi/v1/ticker/price?symbol=BNBUSDT';

var url = burl + query
var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
var ourRequest = new XMLHttpRequest();

ourRequest.open('GET', url, true);

ourRequest.onload = function() {
  console.log(ourRequest.responseText);
  const coinPrice = ourRequest.responseText
  console.log(coinPrice)
}

ourRequest.send();
Andy
  • 61,948
  • 13
  • 68
  • 95

1 Answers1

1
var coinPrice = ourRequest.responseText

Change "const" to "var"

or define it before with the "let" keyword

let coinPrice;
ourRequest.onload = function() {
  console.log(ourRequest.responseText);
  coinPrice = ourRequest.responseText
  console.log(coinPrice)
}
Qgruber
  • 137
  • 1
  • 11