Tuesday, 9 May 2017

Get all key/values pairs using node_redis and nodejs async/await

I'm using node_redis, promisified using Bluebird, with NodeJS 7.10. I'm trying to get all keys in my Redis database with their values. This code works but is smelly, how can I refactor it to be nicer without await that every db.getAsync(key) call be solved?

'use strict'

const {db} = require('../db')
const Promise = require('bluebird')

async function getAllKeysValues () {
  let values = {}
  let promises = []
  let keys = await db.keysAsync('*')
  for (let key of keys) {
    promises.push(db.getAsync(key))
  }
  let resolvedPromises = await Promise.all(promises)
  for (let i = 0; i < keys.length; i++) {
    values[keys[i]] = resolvedPromises[i]
  }
  return values
}



via Luis Felipe LondoƱo

No comments:

Post a Comment