如何重写async/await下的一个函数?

How to rewrite a function under async/await?

我想停止使用异步库,用 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

我要实施的解决方案还包括 bluebird。下面是我将如何使用它。

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
  })
}