Created
January 12, 2017 13:57
-
-
Save medesko/926a539c27ae811d1932303639b6ecc2 to your computer and use it in GitHub Desktop.
NodeJS Promise
This file contains hidden or 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 child_process = require('child_process'); | |
function execute(command) { | |
return new Promise(function(resolve, reject) { | |
doExecute(resolve, reject, command) | |
}); | |
} | |
function executeFile(file, options) { | |
return new Promise(function(resolve, reject) { | |
doExecuteFile(resolve, reject, file, options) | |
}); | |
} | |
function doExecute(resolve, reject, command) { | |
child_process.exec(command, function(error, stdout, stderr){ | |
if (error) { | |
return reject(stderr); | |
} | |
resolve(stdout); | |
}); | |
} | |
function doExecuteFile(resolve, reject, file, options) { | |
child_process.execFile(file, options, function(error, stdout, stderr){ | |
if (error) { | |
return reject(stderr); | |
} | |
resolve(stdout); | |
}); | |
} | |
module.exports = { | |
exec: execute, | |
execFile: executeFile | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment