0

I am working with the CoinMarketCap API and have managed to setup and get server response and am getting the values that I need using the following code

request('GET','https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?CMC_PRO_API_KEY=' + apikey.key + '&symbol=BTC,LTC').then((r1) => {
    var x1 = JSON.parse(r1.target.responseText);
    console.log(x1);

    var json = {
        BTC: x1.data.BTC.quote.USD.price,
        LTC: x1.data.LTC.quote.USD.price,
    }
    console.log(json.BTC, json.LTC);

}).catch(err => {
    console.log(err);
})  

Is there a way that I can refer me 'var json' outside the function or in any other file, if not is there any other way I could get the same values without using this method of linking to the server which would allow me to make these public variables

I cannot by default make it a public variable as to when I do so x1 will not be found and making x1 public the r1 would not be found

Hope the question made sense, I am fairly new to this stuff

  • Not directly. You will need to pass the `json` variable to a function which you call from inside the `then()` handler function. – Rory McCrossan Jul 04 '21 at 16:11
  • Also note that your `json` variable is badly named - that's a plain object contained there, nothing to do with JSON. – Rory McCrossan Jul 04 '21 at 16:12
  • Hey @RoryMcCrossan Thanks for the reply, but do you think you could help me with a little bit of code on what you said in the first comment, and yea I will change the name this is basically a 'test' of my final project so I gave it a simple name – Fatboy Vlogs Jul 04 '21 at 16:17
  • Sure, the two answers I marked as duplicates contain all the information about the subject of handling async data, along with code examples. – Rory McCrossan Jul 04 '21 at 16:19
  • Yea sure, Thanks! will check them out – Fatboy Vlogs Jul 04 '21 at 16:20

0 Answers0