Search code examples
javascriptnode.jsredispromisebluebird

Promisify Redis client


How can I promisify redis so that I could use then?

I have tried to promisify client:

var redis = require('redis');
Promise.promisifyAll(redis.RedisClient.prototype);
var client  = redis.createClient();

client.on('connect', function(){
    console.log('Redis connection is up');

    client.lrange('abc',0,3).then(function(result){
        console.log(result);
        res.send(200)
    });
});

But it responds with error:

client.lrange(...).then is not a function

PS: The callback code works fine, so it means server is running perfectly.


Solution

  • When using promisifyAll, the promisified methods get an -Async suffix:

    client.lrangeAsync('abc',0,3).then(...);
    

    As per the documentation:

    Note that the original methods on the object are not overwritten but new methods are created with the Async-suffix. For example, if you promisifyAll the node.js fs object use fs.statAsync to call the promisified stat method.