I am having trouble assigning the value to btcprice, when I try to log the variable after the http.get it outputs undefined. I understand that http.get is occurring asynchronously, but don't know what to do in order to fix this. Any help would be great! Thank you.
const http = require('http');
var btcprice;
// request api
http.get(
{
host: 'api.coindesk.com',
path: '/v1/bpi/currentprice.json'
},
function(response){
// get data
let body = '';
response.on('data', function(d) { body += d; });
response.on('end', function() {
// manipulate received data
let parsed = JSON.parse(body);
btcprice = parsed.bpi.USD.rate;
});
})