I am trying to access a variable outside of the scope of the function. I am trying to access price outside of the function. Do I have to wait until the request finishes, or do I not have access to the price?
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', "https://api.iextrading.com/1.0/stock/aapl/quote", true);
httpRequest.send();
httpRequest.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
response = JSON.parse(httpRequest.responseText);
console.log(response.latestPrice);
var lastPrice = response.latestPrice;
document.getElementById("StockPrice").innerHTML = lastPrice;
price = lastPrice;
}
}
document.write("outside: " + price);