2

I am trying to pass two values to a function from two async functions and I am not sure how to proceed. Here is the code:

    var btcPriceInUSD;
var priceExchangeMXN;
var btcLink = "https://blockchain.info/ticker";
var exchangeRateLink = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDMXN%22%29&env=store://datatables.org/alltableswithkeys&format=json";

//Get btc price in USD
$.getJSON(btcLink, function(btcData) 
    {   
        btcPriceInUSD = btcData.USD.last;
        //document.write(btcPriceInUSD);
    });     


//Get current USD/MXN exchange rate
$.getJSON(exchangeRateLink, function(exchangeData) 
    {   
        priceExchangeMXN = exchangeData.query.results.rate.Rate;
        //document.write(priceExchangeMXN);
    });     


//Convert btc price to MXN
function convertToMXN(btc,toMXN){
    var result = parseFloat(btc) * parseFloat(toMXN);
    document.write(result);
}


convertToMXN(btcPriceInUSD,priceExchangeMXN)

I know the issue is that I am calling the function outside of the async ones so it is not recieving the numbers and it is giving me a NAN (not a number) but I don't know how I would correctly pass those two parameters that are each retrieved in different functions, is it possible to combine the btcPriceInUSD and priceExchangeMXN in one and call it from there?

Thanks in advance!

Noah-1
  • 396
  • 1
  • 5
  • 20
  • 1
    You might want to have a look at promises and JQuery.deffered object. – Kodz Dec 03 '15 at 06:23
  • The two first answers here explains solutions in a good way: http://stackoverflow.com/questions/14031421/how-to-make-code-wait-while-calling-asynchronous-calls-like-ajax – Kodz Dec 03 '15 at 06:29

2 Answers2

3

Try using $.when() , .then() , substituting returning value at complete function for declaring variables outside scope of asynchronous functions ; also adding an error handler

$.when($.getJSON(btcLink, function(btcData) {   
        return btcData.USD.last
       })     
, $.getJSON(exchangeRateLink, function(exchangeData) {   
        return exchangeData.query.results.rate.Rate
    }))
.then(convertToMXN, function err() {console.log(arguments)})
guest271314
  • 1
  • 15
  • 104
  • 177
1

try this (simply chaining the ajax calls and finally calling the method when both values are available)

var btcPriceInUSD;
var priceExchangeMXN;
var btcLink = "https://blockchain.info/ticker";
var exchangeRateLink = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDMXN%22%29&env=store://datatables.org/alltableswithkeys&format=json";

//Get btc price in USD
$.getJSON(btcLink, function(btcData) 
    {   
        btcPriceInUSD = btcData.USD.last;
        //document.write(btcPriceInUSD);

      //Get current USD/MXN exchange rate
      $.getJSON(exchangeRateLink, function(exchangeData) 
        {   
           priceExchangeMXN = exchangeData.query.results.rate.Rate;
            //document.write(priceExchangeMXN);
           convertToMXN(btcPriceInUSD,priceExchangeMXN);
        });  

    });     

//Convert btc price to MXN
function convertToMXN(btc,toMXN){
    var result = parseFloat(btc) * parseFloat(toMXN);
    document.write(result);
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94