title: Async functions in Node.js module
body: <p>I'm kind of new to JavaScript/Node.js so bear with me. Also my english may not be that good.</p>
  
  <p>I'm trying to write a Node.js module <strong>module.js</strong> with functions that do some long-running work. Kind of like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>var exec = require('child_process').exec;
  
  module.exports.myFunction1 = function(callback) {
      // this function runs for like 3 seconds
      exec('long running shell command' ,function(err,stdout,stderr) {
          callback(stdout);
      })
  };
  
  module.exports.myFunction2 = function(callback) {
      // this function runs for like 1 second
      exec('long running shell command' ,function(err,stdout,stderr) {
          callback(stdout);
      })
  };
  </code></pre>
  
  <p>Now, I also have a <strong>main.js</strong> where I invoke these functions:</p>
  
  <pre class="lang-js prettyprint-override"><code>var module = require('./module.js');
  
  var output1 = module.myFunction1();
  
  var output2 = module.myFunction2();
  </code></pre>
  
  <p>My first problem is that my functions return <em>undefined</em>. I understand that this is because the exec function runs asynchronously and therefore the function returns before exec has finished. This is basically what I want but how can I tell my function that it should only callback when exec has finished?</p>
  
  <p>I also don't want the functions to block node.js when I invoke them in my main.js. So basically, my output of the above code would be...</p>
  
  <pre><code>Output myFunction2: Output2
  Output myFunction1: Output1
  </code></pre>
  
  <p>...because myFunction2() finishes faster than myFunction1().</p>
  
  <p>I tried many, many solutions I found online but nothing seems to work properly.</p>
  
  <p>Thank you very much in advance!</p>
  
  <p><strong><em>--- EDIT ---</em></strong></p>
  
  <p>Ok, I'm having a somewhat correct solution. Right now my code looks like this:</p>
  
  <p><strong>module.js</strong></p>
  
  <pre><code>var Q = require('q');
  require('shelljs/global')
  
  module.exports = {
      myFunction1: function () {
          var deferred = Q.defer();
  
          var result = exec('long running command', {silent:true}).output.toString();
  
          if (ok) {
              deferred.resolve(result);
          }
          else {
              deferred.reject('Error');
          }
  
          return deferred.promise;
      },
  
      myFunction2: function () {
          var deferred = Q.defer();
  
          var result = exec('long running command', {silent:true}).output.toString();
  
          if (ok) {
              deferred.resolve(result);
          }
          else {
              deferred.reject('Error');
          }
  
          return deferred.promise;
      }
  }
  </code></pre>
  
  <p>My <strong>main.js</strong> lloks like this now:</p>
  
  <pre><code>var module = require('./module');
  
  module.myFunction1()
      .then(function (result) {
          console.log('Result 1: ' + result);
      })
      .fail(function (error) {
          console.log(error)
  });
  
  module.myFunction2()
      .then(function (result) {
          console.log('Result 2: ' + result);
      })
      .fail(function (error) {
          console.log(error)
  });
  </code></pre>
  
  <p>And I get the expected output:</p>
  
  <pre><code>Result 1: Output that myFunction1() generated
  Result 2: Output that myFunction2() generated
  </code></pre>
  
  <p>My Problem now is, that myFunction1() always logs before myFunction2(), even if myFunction2() finished first. Did I understood something wrong about Promises? Shouldn't myFunction2() return immediately after it finished?</p>