Boursomaster
Boursomaster

Reputation: 230

How to use asynchronus functions in promises?

I'm using Node.js with Express, and I'm trying to fill 2 arrays by calling another API.

My problem is that my response always return void arrays.

This is in my opinion due to the fact that promises are asynchronous and the callback is not executed before I send the response.

I've tried to implement async/await before functions with then but didn't worked...

app.get('/test', (req, res) => {
  let APIKey = 'Udt4dOLtw4TI7mEhfSZHJ5TbgGC9q7kB';
  let SecretKey = 'HV7bzQrDmUJpVz1UyjWpy2sle9Ly/WYj';
  hitbtc.auth(APIKey, SecretKey);
  let buy = [];
  let sell = [];
  hitbtc.symbols().then(symbols => {
    symbols.forEach((symbol) => {
      let open = [];
      let timeStamp = [];
      let i = 0;
      console.log(symbol.id);
      hitbtc.candles(symbol.id, {limit: 15, period: "H1"}).then(result => {
        result.forEach(element => {
          open.push(element.open);
          timeStamp.push(element.timestamp);
        });
        tulind.indicators.rsi.indicator([open], [14], function (err, results) {
          if (results[0] > 80) {
            console.log("sell : ", symbol.id, " RSI value : ", results[0]);
            sell.push(symbol.id);
          } else if (results[0] < 20) {
            console.log("buy : ", symbol.id, " RSI value : ", results[0]);
            buy.push(symbol.id);
          }
        });
      })
        .catch(e => {
          //console.log(e);
        });
    });
    res.status(200).json({
      success: true,
      buy: buy,
      sell: sell
    })
  });
});

I'm expecting that buy and sell array to be filed in the res.status(200) response.

Thanks for your help.

Upvotes: 1

Views: 234

Answers (1)

brun0
brun0

Reputation: 76

You should move your response inside the promise, otherway it will be executed before you get the results from the API

Example using your code:

app.get('/test', (req, res) => {
    let APIKey = 'Udt4dOLtw4TI7mEhfSZHJ5TbgGC9q7kB';
    let SecretKey = 'HV7bzQrDmUJpVz1UyjWpy2sle9Ly/WYj';
    hitbtc.auth(APIKey, SecretKey);
    let buy = [];
    let sell = [];
    hitbtc.symbols().then(symbols => {
        symbols.forEach((symbol) => {
            let open = [];
            let timeStamp = [];
            let i = 0;
            console.log(symbol.id);
            hitbtc.candles(symbol.id, {limit: 15, period: "H1"}).then(result => {
                result.forEach(element => {
                    open.push(element.open);
                    timeStamp.push(element.timestamp);
                });
                tulind.indicators.rsi.indicator([open], [14], function (err, results) {
                    if (results[0] > 80) {
                        console.log("sell : ", symbol.id, " RSI value : ", results[0]);
                        sell.push(symbol.id);
                    } else if (results[0] < 20) {
                        console.log("buy : ", symbol.id, " RSI value : ", results[0]);
                        buy.push(symbol.id);
                    }
                });

                res.status(200).json({
                    success: true,
                    buy: buy,
                    sell: sell
                });

            })
            .catch(e => {
                //console.log(e);
            });
        });
    });
});

Upvotes: 0

Related Questions