Sunday 11 June 2017

Node.js Promises async using popsicle

I have a function which makes 1 API call returning a Promise. Whats the best way to convert this code to make n calls for different timestamps. I would like to return an object with the unaggregated data stored by key timestamp.

This code runs for i = 1 and histData is undefined.

const getEthPriceHistorical= (toSymbol) => {
    var histData = {};
    var ts;
    for (let i = 1; i < 5; i++){
        ts =  new Date(new Date().getTime() - (24*i * 60 * 60 * 1000));

        if (typeof toSymbol === 'string') {
           toSymbol = toSymbol.toUpperCase();
        } else {
            toSymbol = 'USD,EUR,GBP,CHF,THB,AUD,INR';
        }
        ***** WHAT TO DO WITH THIS SINGLE PROMISE *****
        return popsicle.request({
           method: 'POST',
           url: 'https://min-api.cryptocompare.com/data/pricehistorical',

           query: {
            fsym: 'ETH',
            tsyms: toSymbol,
            timestamp: ts
           }
       })
         .use(popsicle.plugins.parse(['json']))
         .then(resp => resp.body)
         .then(data => {
            const symbols = Object.keys(data);
            histData.ts = data;
            console.log(ts, data);
        });
  }
  return histData;
}



via codervince

1 comment:

  1. Invest in Ethereum on eToro the World’s #1 Social Trading Network...

    Join millions who have already discovered easier strategies for investing in Ethereum...

    Learn from profitable eToro traders or copy their positions automatically

    ReplyDelete