3

Using the npm module 'redis', I've had no problem using utils promisify for single commands.

const { promisify } = require("util");
const redis = require("redis");
const client = redis.createClient();

const hmgetAsync = promisify(client.hmget).bind(client),

Now I want to be able to use the .multi and .batch methods.

Here is an example of one of my helper functions that uses .batch without promisify and non of the promisify versions of commands.

const getData = async (ids) => {
    const batch = client.batch();

    ids.forEach(id => {
        batch.hgetall(id)
    });

    return new Promise((resolve, reject) => {
        batch.exec((err, replies) => {
            if (err) {
                reject(err);
            }

            const data = replies
                // missing keys require filtering
                .filter(it => !!it)
                .map(dataPoint => _toDataPoint(dataPoint));

            resolve(data);
        })
    })
};

Works no problem, but I'm curious if it is possible to use promisify with either .multi or .batch

Many thanks,

Aaron
  • 193
  • 7

1 Answers1

4

Here is an example of promisified version of multi with an array of commands as an argument:

const { promisify } = require("util");
const redis = require("redis");
const client = redis.createClient();

const multiPromisified = commands => {
        const multi = client.multi(commands);
        return promisify(multi.exec).call(multi);
}

multiPromisified([['get', 'key1'], ['get', 'key2']]);
Ihor Sakailiuk
  • 5,642
  • 3
  • 21
  • 35