Forked from EricLondon/gist:f5e4722c15d1e9d6a7d1
Last active
August 29, 2015 14:24
-
-
Save servel333/d2349b221daac01132b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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