0

I am trying to hit an api and setting data to an object properties. I am setting few properties inside javascript promise and then pushing object in array outside the promise. So now object pushed before values returned from promise. How to solve this? My code is here.

const getUserExchangeAccountDetails = function(props, params, callback) {
const client = new bittrex(constants.bittrexApiKey, constants.bittrexSecretKey);
let responseArray = [];
let tempArr = [];
let otherArr = [];
let coinNumber = 0;

return new Promise(function(resolve, reject) {
    client.getbalances(function (err, response, taskcallback) {
        if (err) {
            reject(err);
        } else {
            let coinsList = response.result;
            console.log("Results-----", response.result);

            for(let i = 0; i < response.result.length; i++ ){
                let coin = {
                        currency : "",
                        balance : 0,
                        usdPrice : 0,
                        coinsInUsd : [],
                        totalCoins : 0,
                };

                if(response.result[i].Currency != null || 
                    response.result[i].Currency != ""){
                        coin.currency = response.result[i].Currency;
                }

                if(response.result[i].Balance > 0 ){
                    coin.balance = response.result[i].Balance;
                }

                if(coin.currency != "" && coin.balance > 0){
                        coinNumber = coinNumber + 1;
                        coin.totalCoins = coinNumber;
                        if (coin.currency != 'BTCP') {
                            currenciesService.getCurrencyLatestInfo(coin.currency).then((data) => {
                                coin.usdPrice = data;
                                let usdData = conversionIntoDollar.coinsConversionInUsd(coin.balance, coin.usdPrice);
                                coin.coinsInUsd.push(usdData);
                            });
                        }
                        responseArray.push(coin);
                        console.log(responseArray);

                }
            }
            resolve(responseArray);  
        } //else
    });
})

}

Here is output of my code, values returning form promises are empty, rest of the properties are working fine.

Output Data screenshot

0 Answers0