I'm kind of new to node.js/JavaScript so bear with me. I'm implementing a node module named module.js like this:
function run(cmd, arg) {
var exec = require('child_process').exec;
return new Promise(function(resolve, reject) {
exec(cmd + ' ' + arg, function(error, stdout, stderr) {
if (error)
reject(error);
else
resolve(stdout);
});
});
}
function getCon1() {
run('ls', '')
.then(function(result) {
return result;
}, function(err) {
console.log(err);
});
}
exports.getCon1 = getCon1;
function getCon2() {
run('ls', '-a')
.then(function(result) {
return result;
}, function(err) {
console.log(err);
});
}
exports.getCon2 = getCon2;
And my main.js
, where I'm using my module, looks like this:
var mod = require('./module');
console.log(mod.getCon1());
console.log(mod.getCon2());
My problem is that my two getter function return undefined in my main.js. For me, this is kind of irritating because I thought thats exactly what promises are for: The getter functions return in then()
when the run()
function completes.
Can anybody help me out here real quick? Thank you!