EventEmitter位于Promises链中间

时间:2014-08-09 07:38:12

标签: node.js promise bluebird

我正在做一些事情,包括按顺序运行child_process.spawn()序列(进行一些设置,然后运行调用者感兴趣的实际多肉命令,然后进行一些清理)。

类似的东西:

doAllTheThings()
  .then(function(exitStatus){
    // all the things were done
    // and we've returned the exitStatus of
    // a command in the middle of a chain
  });

doAllTheThings()类似于:

function doAllTheThings() {
  runSetupCommand()
    .then(function(){
      return runInterestingCommand();
    })
    .then(function(exitStatus){
      return runTearDownCommand(exitStatus); // pass exitStatus along to return to caller
    });
}

我在内部使用child_process.spawn(),它会返回EventEmitter,并且我有效地将close事件的结果从runInterestingCommand()返回到来电者。

现在我还需要从stdout和stderr向调用者发送data个事件,这些事件也来自EventEmitters。有没有办法让(Bluebird)Promises使用它,或者它们只是阻碍了发出多个事件的EventEmitters?

理想情况下,我希望能够写下:

doAllTheThings()
  .on('stdout', function(data){
    // process a chunk of received stdout data
  })
  .on('stderr', function(data){
    // process a chunk of received stderr data
  })
  .then(function(exitStatus){
    // all the things were done
    // and we've returned the exitStatus of
    // a command in the middle of a chain
  });

我能想到使我的程序工作的唯一方法是重写它以删除promise链,只需在包含setup / teardown的东西中使用原始EventEmitter,如:

withTemporaryState(function(done){
  var cmd = runInterestingCommand();
  cmd.on('stdout', function(data){
    // process a chunk of received stdout data
  });
  cmd.on('stderr', function(data){
    // process a chunk of received stderr data
  });
  cmd.on('close', function(exitStatus){
    // process the exitStatus
    done();
  });
});

但是,由于EventEmitters在Node.js中非常普遍,我无法帮助,但我认为我应该能够让它们在Promise链中运行。有线索吗?

实际上,我想继续使用Bluebird的原因之一是因为我想使用Cancellation功能来允许从外部取消运行命令。

1 个答案:

答案 0 :(得分:28)

有两种方法,一种提供您最初要求的语法,另一种提供代理。

function doAllTheThings(){
     var com = runInterestingCommand();
     var p = new Promise(function(resolve, reject){
         com.on("close", resolve);
         com.on("error", reject);
     });
     p.on = function(){ com.on.apply(com, arguments); return p; };
     return p;
}

这将允许您使用所需的语法:

doAllTheThings()
  .on('stdout', function(data){
    // process a chunk of received stdout data
  })
  .on('stderr', function(data){
    // process a chunk of received stderr data
  })
  .then(function(exitStatus){
    // all the things were done
    // and we've returned the exitStatus of
    // a command in the middle of a chain
  });

然而,IMO这有点误导,可能需要传递代表:

function doAllTheThings(onData, onErr){
     var com = runInterestingCommand();
     var p = new Promise(function(resolve, reject){
         com.on("close", resolve);
         com.on("error", reject);
     });
     com.on("stdout", onData).on("strerr", onErr);
     return p;
}

可以让你这样做:

doAllTheThings(function(data){
    // process a chunk of received stdout data
  }, function(data){
    // process a chunk of received stderr data
  })
  .then(function(exitStatus){
    // all the things were done
    // and we've returned the exitStatus of
    // a command in the middle of a chain
  });