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,
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']]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With