0

I am getting undefined for the return statement in this function. Being new to the language, I searched it up and I found out that the function has to be async so that it returns a Promise.

Can someone help me understand why does everything until the console.log(qty) work normally but the return statement is executed before all of that.

Also, what would be the effiecient way to get the result of this function in a variable somewhere else. I tried the async/await and it returns the result in a Promise - which I still have to read on how to parse it.

//convert quantity to correct decimals
function binanceNormalizeQuantity(symbol, quantity) {
    requestApiData('GET', 'https://api.binance.com/api/v1/exchangeInfo').then((response) => {
        response = JSON.parse(response.target.responseText);
        var qty;
        for (var i = 0; i < response["symbols"].length; i++) {
            if (response["symbols"][i]["symbol"] == symbol) {
                var stepSize = Math.log10(1 / parseFloat(response["symbols"][i]["filters"][2]["stepSize"]));

                qty = Math.floor(quantity * 10**stepSize)/10**stepSize;
            }
        }
        console.log(qty);
        return qty;
    });
}
Salman Fazal
  • 559
  • 7
  • 22
  • Returning the result of an asynchronous operation like a simple `return` does not make sense. – Pointy Mar 25 '19 at 22:47
  • 2
    You are missing `return requestApiData(...`. – Felix Kling Mar 25 '19 at 22:47
  • 1
    You must think that return statement is for `binanceNormalizeQuantity`, but it's not, it's for the arrow function. `binanceNormalizeQuantity` has no return statement, thus it returns the default which is `undefined`. – Paul Mar 25 '19 at 22:48
  • @Paulpro Thanks for the explanation! May I ask, if I add the return like how Felix mentioned, I get a Promise object. How do I get the specific value from the [[PromiseValue]] ? – Salman Fazal Mar 25 '19 at 22:56
  • You can use async/await or use Promise#then `binanceNormalizeQuantity( someSymbol, someQuantity ).then( result => { console.log( result ); } );` – Paul Mar 25 '19 at 22:58
  • So here's the thing, I do get the console.log result even if I don't use the .then() .. what I am trying to do is pass the result into a different function. In the new function, it still says undefined while in the binanceNormalizeQuantity() function I get the Promise object (which holds the result). @Paulpro – Salman Fazal Mar 25 '19 at 23:04
  • Just replace console.log with that under function. You can even just do `binanceNormalizeQuantity( someSymbol, someQuantity ).then( someOtherFunction )`. – Paul Mar 25 '19 at 23:13
  • I'm normalizing three different values, which I may or may not have at times. I also call one or more of these normalize functions in several other functions when need be. I don't see as what you mentioned to be an option. I come from a python background and this is completely new for me tbh. All I want to do is do this math in a different function and get back a result when I need. – Salman Fazal Mar 25 '19 at 23:29
  • I don't understand why any of that is a problem. It doesn't matter where you call `binanceNormalizeQuantity( someSymbol, someQuantity )` or how you use the value that the Promise it returns gives you, you can do anything you want with it. – Paul Mar 26 '19 at 01:59

0 Answers0