0

I have to call an asynchronous function multiple times (but the number of calls is dynamic). For some reason, using this approach for the Promise.all() the functions in the array getPricesCalls don't actually fire.

var getPrice = function (exchange, product) {
    var priceData = {};
    return new Promise(function (resolve, reject) {
        if (exchange == 'GDAX') {
            const gdaxPublicClient = new Gdax.PublicClient(product);
            gdaxPublicClient.getProductTicker((error, response, data) => {
                if (error) {
                    reject(Error(error));
                } else {
                    console.log("GDAX ", product, ": ", data['price']);
                    priceData[exchange] = {
                        product: parseFloat(data['price'])
                    }
                    resolve(priceData);
                }
            });
        }

        if (exchange == 'POLONIEX') {
            poloniex.returnTicker(function (err, data) {
                if (err) {
                    reject(Error(error));
                } else {
                    console.log("POLONIEX ", product, ": ", data[product]['last']);
                    priceData[exchange] = {
                        product: parseFloat(data[product]['last'])
                    }
                    resolve(priceData);
                }
            });
        }
    });
}

var exchanges = ['GDAX', 'POLONIEX'];
var gdaxProducts = ['BTC-USD', 'ETH-BTC', 'LTC-BTC'];
var poloniexProducts = ['USDT_BTC', 'BTC_ETH', 'BTC_LTC'];
var products = {
    "GDAX": gdaxProducts,
    "POLONIEX": poloniexProducts
}
var gdaxPrices = {};
var poloPrices = {};

var getPricesCalls = [];
for (var exch in exchanges) {
    for (var product in products) {
        var functionCall = getPrice(exch, product);
        getPricesCalls.push(functionCall);
    }
}

// Manually creating the array works:
// var getPricesCalls = [getPrice(exchanges[0], gdaxProducts[0]), getPrice(exchanges[1], poloniexProducts[1])];

Promise.all(getPricesCalls).then(function (results) {
    console.log(results);
}, function (err) {
    console.error(err);
});
JBaczuk
  • 13,886
  • 10
  • 58
  • 86
  • 1
    You're passing `"0"` instead of `"GDAX"` and `"1"` instead of `"POLONIEX"` (and similarly for the products). See the linked question's answers for why. I suggest the first step when you run into something like this that isn't working should be to step through the code with a debugger; there's almost certainly one built into your IDE, one built into Node (using Chrome/Chromium as its UI), and one built into all major browsers. – T.J. Crowder Nov 30 '17 at 17:06
  • 1
    Thanks to @T.J. Crowder I was able to create the array using a .forEach loop over the gdaxProducts and poloniexProducts arrays: var getPricesCalls = []; for (var exch in products) { products[exch].forEach(function(product) { var functionCall = getPrice(exch, product); getPricesCalls.push(functionCall); }) } – JBaczuk Nov 30 '17 at 17:17

0 Answers0