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;
}

upvote
  flag
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
upvote
  flag
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

0 Answers 11

Not the answer you're looking for? Browse other questions tagged or ask your own question.