1

If I move socket.emit outside of that function, the socket emit event happens. However, I need to get a hold of 'values' data, which is in the promise call back. The socket emit event in that case doesn't happen.

app.io.on('connection', function(socket) {
  setInterval(function() {
    var bitfinex = new Promise(
      function(resolve, reject) {
        const orderBook = 'https://api.bitfinex.com/v2/book/tBTCUSD/P0'
        axios.get(orderBook)
          .then(function(response) {
            resolve(response)
          })
          .catch(function(error) {
            reject(error)
          });
      });

    var bitmex = new Promise(
      function(resolve, reject) {
        const orderBook = 'https://www.bitmex.com/api/v1/orderBook/L2?symbol=xbt&depth=25'
        axios.get(orderBook)
          .then(function(response) {
            resolve(response)
          })
          .catch(function(error) {
            reject(error)
          });
      });
    Promise.all([bitmex, bitfinex]).then(values => {
      socket.emit('feed', {
        data: values
      })
    });
  }, 3000)
hewugub
  • 11
  • 2

1 Answers1

0

Axios is returning a promise so you don't need to create new Promise and you should catch your error if there is any:

app.io.on(
  'connection', 
  function (socket) {
    setInterval(
      function () {
        Promise.all(
          [
            axios.get('https://api.bitfinex.com/v2/book/tBTCUSD/P0'),
            axios.get('https://www.bitmex.com/api/v1/orderBook/L2?symbol=xbt&depth=25')
          ]
        ).then(values =>
          socket.emit(
            'feed',
            {
              data: values
            }
          )
        )
        .catch(
          e => console.warn("something went wrong:",e)
        )
      },
      3000
    )
  }
);
HMR
  • 37,593
  • 24
  • 91
  • 160