【问题标题】:node-redis promisification using bluebird使用蓝鸟的 node-redis 承诺
【发布时间】:2016-02-16 02:13:29
【问题描述】:

我正在尝试对 node-redis 包使用 Promise,但我不能使用 on.connect() 方法。

var redis = require("redis");
var client = redis.createClient();

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

// callback version
app.get('/redis', function(req, res) {
  client.set("foo_rand000000000000", "some fantastic value", function(err, reply) {
    if (err) {
      console.log(err);
    } else {
      console.log(reply);
    }
    res.end();
  });
});

// promisified version
app.get('/redis', function(req, res) {
  return client.setAsync("foo_rand000000000000", "some fantastic value").then(function(return) {
    console.log(return); // returns OK
    client.quit();
  });
});

但是我被下面的那个卡住了,我怎么能答应呢?

// example
client.on("connect", function () {
    client.set("foo_rand000000000000", "some fantastic value", redis.print);
    client.get("foo_rand000000000000", redis.print);
});

我尝试了下面的一个但它不起作用,我在命令行上看不到响应:

app.get('/redis', function(req, res) {
  return client.onAsync("connect").then(function(res) {
    console.log(res);
    res.end();
  });
});

【问题讨论】:

    标签: node.js promise node-redis


    【解决方案1】:

    和 Freyday siad 一样,on 不是异步方法,而是一个事件发射器,所以我强烈建议你不要承诺它,但是,如果你坚持,你可以这样做:

    let _connectResolve, _connectReject, onConnected = new Promise((resolve, reject) => {
        _connectResolve = resolve;
        _connectReject = reject;
     }), redis = require("redis"),
     client = redis.createClient();
    
    client.on('connect', _connectResolve);
    
    // usage example:
    onConnected.then(() => {
      client.setAsync("foo_rand000000000000", "some fantastic value").then(redis.print);
        client.getAsync("foo_rand000000000000").then(redis.print);
    });
    

    如果您担心的是,您必须等待客户端连接才能获取/设置内容,您可以将所有调用链接到 onConnected 承诺。例如:

    app.get('/redis', (req, res) => {
      onConnected
        .then(() => client.setAsync("foo_rand000000000000", "some fantastic value"))
        .then(() => res.end());
    });
    

    【讨论】:

      【解决方案2】:

      client.on() 方法来自 EventEmitter 原型,redis 客户端可能继承自该原型。您也可以尝试遍历原型链并对其进行承诺,但我可能会像这样自己处理它:

      function onConnect(client) {
        return new Promise(function(resolve, reject) {
          client.on('connect', function(err) {
            if (err) return reject(err)
            return resolve()
          })
        })
      }
      

      那么你就可以这样使用它了:

      app.get('/redis', function(req, res) {
        return onConnect(client)
          .then(function() {
            return client.setAsync("foo_rand000000000000", "some fantastic value")
          })
          .then(function(result) {
            console.log(result); // returns OK
            client.quit();
            res.end();
          });
        });
      });
      

      【讨论】:

        猜你喜欢
        • 2014-12-07
        • 2015-07-18
        • 1970-01-01
        • 2015-09-06
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 2014-11-06
        • 2015-02-13
        相关资源
        最近更新 更多