3

I want to stop using the async library, replacing it with vanilla js.

const async = require('async')

function getTopicsData(tids, Uid, callback) {
  async.map(tids, (tid, next) => {
    redis.hgetall(`topic:${tid}`, (err, topics) => {
      redis.sismember(`topic:${tid}:subscribers`, Uid, (err, subscriber) => {
        topics.subscriber = !!subscriber

        next(false, topics)
      })
    })
  }, callback)
}

module.exports = getTopicsData
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
NCHAryON
  • 61
  • 1
  • 6

1 Answers1

2

The solution I would implement would also include bluebird. Here is how I would use it.

const Promise = require('bluebird')

const hgetall = Promise.promisify('redis.hgetall')
const sismember = Promise.promisify('redis.sismember')

module.exports = async function (tids, Uid) {
  return Promise.map(tids, async function (tid) {
    let {topics, subscriber} = await Promise.props({
      topics: hgetall(`topic:${tid}`),
      subscriber: sismember(`topic:${tid}:subscribers`, Uid)
    })

    topics.subscriber = !!subscriber

    return topics
  })
}
InternalFX
  • 1,475
  • 12
  • 14