雷迪斯多蓝鸟承诺
你知道一个使用Redbird客户端的多重交易命令蓝鸟承诺的方法吗?
因为,下面的代码永远不会结束。
var $redis = require('redis'), $p = require('bluebird'), $r = $p.promisifyAll($redis.multi()); $r.setAsync('key', 'test') .then(function(reply, data) { // ... }); $r.exec(function() { $r.quit(); process.exit(); });
只有这个命令不需要挂起的东西就是在一个promisified连接之前得到多个。
var $redis = require('redis'), $p = require('bluebird'), $r; // this is important for bluebird async operations! $r = $p.promisifyAll($redis.createClient.apply(this, arguments)); // multi also need to be promisifed with the promisified conn above $r = $p.promisifyAll($r.multi()); $r.setAsync('key', '0').then(function(data) { }); $r.incrAsync('key'); // all of the above commands pipelined above will be executed with this command $r.execAsync().then(function() { $r.quit(); // this will make the console app (or whatever app) quit process.exit(); });
有没有办法在这些块完成后运行exec?
嗯,连锁吧:
$r.pfaddAsync('key', item) .then(function(result) { // marked if (result === 0) { $r.incrAsync('dup'); } else { $r.incrAsync('unq'); } $r.exec(); });
甚至可能是
$r.pfaddAsync('key', item) .then(function(result) { // marked if (result === 0) { $r.incrAsync('dup'); } else { $r.incrAsync('unq'); } }) .then($r.exec);
或者,如果你想在incrAsync
完成后执行它,那就是了
$r.pfaddAsync('key', item) .then(function(result) { return $r.incrAsync(result === 0 ? 'dup' : 'unq'); // ^^^^^^ }) .then($r.exec);
当需要将exec
作为方法调用时, .then($r.exec)
可能不起作用,请改用.then($r.exec.bind($r))