0

The function below is supposed to return a number(ie: 1.3023123, or 0.0005300). However, The function is not returning anything at all. Ticker[pair] seems to be working, and the console does log the expected value.

function get_coin_price_BTC(coin) {
  var x = "";

  binance.prices(function(ticker) {
    var pair = coin + "btc";
    pair = pair.toUpperCase();


    console.log("Price of " + pair + ": " + ticker[pair]);
    x = ticker[pair];
    console.log("x: " + x);


  });

  return x;
}
connexo
  • 53,704
  • 14
  • 91
  • 128
user1698144
  • 754
  • 4
  • 13
  • 36
  • If the 'binance.price' is an async functions so the order execution is not : 1. binance.price function 2 Then return X As you should think it's the quicker first, so return X will be calle before the function. You have to use callback inside binance.price to use X somewhere else. – nodeover Jan 14 '18 at 09:35
  • NodeOver is right, you have one of those timing error linked to the async specificity of node. Long story short put your return in the callback. In your exemple, you return the value before binance.prices actually finish, that’s your issue – Ernest Jones Jan 14 '18 at 09:56

0 Answers0