0

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!

Since001
  • 81
  • 1
  • 8
  • They return undefined because they don't have return statements. Promises don't make asynchronous code synchronous, they just give you an object you can pass around and attach callbacks to. – Quentin Jan 14 '16 at 21:35
  • You're not returning anything from `getCon1` or `getCon2`, so of course that logs `undefined`. You should return the promise instead, like `function getCon1() { return run('ls', ''); }`. Then you can do `mod.getCon1().then( function ( result ) { console.log( result ); } );` outside the module. – Paul Jan 14 '16 at 21:37
  • @Paulpro Thanks for you answer! This is working but I want to do more in `getCon1()`, kinda like this: `function getCon1() { run('ls', '') .then(function(result) { if (result.search(/\bstring I#n lokking for in output\b/) > -1) { return 'found string'; } }); }` – Since001 Jan 14 '16 at 21:55
  • @Since001 Your should still put `return` in front of `run('ls', '').then( ... )`, that way you are still returning a promise and whatever you return from inside the `then` will be available to the caller when they do `mod.getCon1().then( ...`. – Paul Jan 14 '16 at 22:35
  • @Since001 Promise are chainable, because `then` returns a new promise, so you can do `run('ls','').then( function ( result ) { return next_value or promise_of_next_value } ).then( function ( next_value ) { return third_value or promise } ).then(...).then(...)`. In your case at some point in the chain you want to return from `getCon1` so that the rest of the chain can exist outside your module. You can have any number of `.then`s inside `getCon1`, but once you're using promises you have to stay in them, so you'll need to use `.then` outside the module too after calling `getCon1`. – Paul Jan 14 '16 at 22:46
  • @Paulpro Oh my god, now I get it! Feeling a little bit stupid right now ;) Thank you very much! – Since001 Jan 14 '16 at 23:53
  • @Since001 You're welcome – Paul Jan 15 '16 at 00:07

0 Answers0