I do what the sequence of the sequence child_process.spawn() includes in order (to do some tweaking, and then run the actual meat command that the caller is interested in, then do some cleanup).
Something like:
doAllTheThings() .then(function(exitStatus){
Where doAllTheThings() is something like:
function doAllTheThings() { runSetupCommand() .then(function(){ return runInterestingCommand(); }) .then(function(exitStatus){ return runTearDownCommand(exitStatus);
Internally, I use child_process.spawn() , which returns an EventEmitter , and I effectively return the result of the close event from runInterestingCommand() back to the caller.
Now I also need to send data events from stdout and stderr to the caller, which are also from EventEmitters. Is there a way to do this work with (Bluebird) Promises, or do they just interfere with EventEmitters that emit more than one event?
Ideally, I would like to write:
doAllTheThings() .on('stdout', function(data){
The only way I can work on my program is to rewrite it to remove the promise chain, and just use the raw EventEmitter inside what wraps the install / disable, something like:
withTemporaryState(function(done){ var cmd = runInterestingCommand(); cmd.on('stdout', function(data){
But since EventEmitters are so common in all Node.js, I can't help but think that I should be able to get them to work in Promise chains. Any clues?
In fact, one of the reasons I want to use Bluebird is because I want to use the revoke functions to allow cancellation of the current command from the outside.
d11wtq Aug 09 '14 at 7:38 a.m. 2014-08-09 07:38
source share