0

I'm struggling to access the currrentPrice variable outside of this API call. How is it done?

var currentPrice
//Get the curent market price
var marketSymobl = checkOrders[i].symbol.split(" - ")[0].toUpperCase()
console.log(checkOrders[i].symbol)
finnhubClient.quote(marketSymobl, (error, data, response) => {
  currentPrice = data.c

});

console.log(currentPrice)
Barmar
  • 741,623
  • 53
  • 500
  • 612
Calin89
  • 47
  • 5
  • You can't. The API call runs asynchronously, you can only access the variable in code that runs after the response is received. – Barmar Nov 10 '21 at 21:26
  • Hi Barmar. Thank you very much for you valuable input. Is there no way to access the data from the request outside of it? – Calin89 Nov 11 '21 at 14:04
  • You can access it later, but not immediately after sending the request. – Barmar Nov 11 '21 at 17:12
  • Think of it like this: You order something from Amazon.com. If you then go to your doorstep, there won't be anything there. But if you wait a day or so, it will be there. – Barmar Nov 11 '21 at 17:15

0 Answers0