2

I have this function that returns a Promise which resolves to an object that is later inserted into db.

lastPrice (crypto) {
    return axios.get('https://www.bitmex.com/api/v1/instrument/active').then(res => {
      return _.map(_.filter(res.data, instrument => isMatch(instrument, crypto)), y => {
        return { exchange: getExchangeName(y.symbol), order: getOrder(y.symbol), crypto, lastPrice: getLastPrice(y) }
      })
    }).catch((e) => { Promise.resolve() })
  }

isMatch, getExchangeName, getOrder and getLastPrice used to be sync functions. Now I need getLastPrice to be async.

function getLastPrice (instrument) {
  const regex = /^(ETH)[FNGQHUJVKXZM]\d{2}$/
  let regexResult = regex.exec(instrument.symbol) || []
  if (regexResult.length > 0) {
    const app = require('../../app')
    return app.service('exchanges').find({
      query: {
        exchange: 'BMEXperp',
        crypto: 'ETH'
      }
    }).then(res => {
      if (res.total === 0) {
        return 0
      }
      return res.data[0].lastPrice * instrument.lastPrice
    })
  } else {
    return instrument.lastPrice
  }

I need to keep the same functionality but with an async lastPrice(crypto). Basically getLastPrice should still return a Promise that resolves to an object with no promises in it.

My idea is not to modify the function that calls lastPrice, which is this one:

exchange.lastPrice(crypto).then(res => {
      res = res || []
      res.forEach(element => {
        exchangesService.create(element)
      })
    })
3
  • 2
    in a .then you don't need to specifically return Promise.resolve(x) ... since .then always returns a Promise anyway - this isn't your issue but makes your code harder to read Commented Aug 5, 2019 at 0:56
  • What is it currently returning? Commented Aug 5, 2019 at 1:46
  • it returns a Promise that resolves into { exchange: 'BMEXSep', order: 6, crypto: 'ETH', lastPrice: Promise { <pending> } } and I need lastPrice to be a number Commented Aug 5, 2019 at 1:50

1 Answer 1

3

Missing part is Promise.all.

If you can use async/await then I think something like this is what you want:

lastPrice (crypto) {
  return axios.get('https://www.bitmex.com/api/v1/instrument/active')
    .then(res => {
      const filteredList = _.filter(res.data, instrument => isMatch(instrument, crypto))
      const promises = _.map(filteredList, async (y) => {
        return {
          exchange: getExchangeName(y.symbol),
          order: getOrder(y.symbol),
          crypto,
          lastPrice: await getLastPrice(y),
        }
      })
      return Promise.all(promises)
    }).catch((e) => { Promise.resolve() })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.