Skip to content

Instantly share code, notes, and snippets.

@servel333
Forked from EricLondon/gist:f5e4722c15d1e9d6a7d1
Last active August 29, 2015 14:24
Show Gist options
  • Save servel333/d2349b221daac01132b6 to your computer and use it in GitHub Desktop.
Save servel333/d2349b221daac01132b6 to your computer and use it in GitHub Desktop.
var run_cmd = function(cmd, args) {
var promise = require('bluebird');
return new promise(function (resolve, reject) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString() });
//child.stdout.on('end', function(){ resolve(resp); });
child.on('close', function(exitCode){ resolve(resp); }); // < This might be more approperate
child.on('error', function(err){ reject(resp); });
});
}
// Example of use
run_cmd(...)
.then(function(resp){})
// Adding to a existing Promise chain (recommended)
Promise...
.then(...)
...
.then(function(){
return run_cmd(...);
})
.then(function(resp){
...
})
// Adding to a existing Promise chain
Promise...
.then(...)
...
.then(function(){
return run_cmd(...);
.then(function(resp){
...
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment