我在其最基本的阶段有以下nodejs代码:

var bluebird = require("bluebird"),
    redis = require('redis');

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
function printOutput(data) {
    console.log('Output:' + data);
}
function printRedisPop(data) {
   if (data !== null) {
       console.log('Still going...');
       return 1;
   } else {
      console.log('Nothing left');
      return 0;
   }
}
// redis is the usual node_redis object. Do what you usually do with it:
var client = redis.createClient(6379,'127.0.0.1');
client.on("error", function (err) {
    console.log("uh oh", err);
});

for (var i=0;i<10;i++) {
// All your redis commands return promises now.
    client.lpushAsync('x', i)
        .then(printOutput)
        .catch(console.log);
}
var x = function() {
    return bluebird.resolve().reflect().then(function(){
        return 'done';
    });
}

for (var i=0;i<100;i++) {
// All your redis commands return promises now.
    client.rpopAsync('x')
        .then(printRedisPop)
        .catch(console.log);
}

bluebird.resolve().reflect().then(function(){
    console.log('done');
});

client.quit();

我想要做的是使用redis Lpush和Rpop的简单FIFO队列 . 在PHP中它运行良好,但我试图将它与Node一起使用 . 当promise发回时,每个console.log部分最终都会进行操作 . 我的问题是我对承诺设计的概念非常新鲜 . 我知道这是一种有效的方法来进行回调,异步,并解决由于then / catch引起的问题 .

理论上这里是 printRedisPop 如果数据为空(即rpop没有任何东西)我可以在client.quit()和process.exit();

(因此,为什么第二个for循环是100,因为我知道它将超过我投入的10个项目 . )

我想要做的是从redis运行"rpop"项目的无限循环并处理它们并继续这样直到列表为空 . 一旦空了,我还有其他事情要做 - 因此我不想连续地将它们链接在 then() 函数中,或者使用process.exit终止应用程序 . 我很想抛出一个错误并 grab 它退出,但我听说这也是不好的做法 . 我试图做的一件事是"wait"在解决/反射并检查then()函数值然而我得到了什么:

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }