【发布时间】: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