title: When to make a function deferred using Promises
body: <p>I'm using the Q node library for Promises, question I think can apply to the Bluebird lib as well.</p>
  
  <h2>Context</h2>
  
  <p>I have a few function calls to make to both my own custom functions and node.js <code>fs</code> style async functions.</p>
  
  <p>if I'm making a call to a function like this:</p>
  
  <blockquote>
    <p>sync function</p>
  </blockquote>
  
  <pre><code>do_something = function (input) {
    // assign variables, do other sync stuff 
  }
  </code></pre>
  
  <p>and need the above to take place <code>before</code> this function:</p>
  
  <blockquote>
    <p>sync function</p>
  </blockquote>
  
  <pre><code>do_something_else = function (input) {
    // assign variable, do other sync stuff
  }
  </code></pre>
  
  <p>and <code>then</code> need to call a native node function similar to:</p>
  
  <blockquote>
    <p>async function</p>
  </blockquote>
  
  <pre><code>writeData = function (obj, callback) {
    var deferred = Q.defer();
    fs.writeFile(obj.file, obj.datas, function (err, result) {
      if (err) deferred.reject(err);
      else deferred.resolve('write data completed');
    });
    return deferred.promise.nodeify(callback);
  }
  </code></pre>
  
  <p>and <code>finally</code> need the above to take place <code>before</code> this function:</p>
  
  <blockquote>
    <p>sync function</p>
  </blockquote>
  
  <pre><code>do_something_last = function (input) {
    // assign variable, do other sync stuff
  }
  </code></pre>
  
  <h2>Question</h2>
  
  <p>Is the 'right' thing to do here, to make all my functions 'deferred' or promise aware so I can make sure that they are called in sequence or in the correct order?
  like so:</p>
  
  <pre><code>do_something(variable)
  .then(do_something_else)
  .then(writeData)
  .then(do_something_last)
  .done();
  </code></pre>
  
  <p>or should I just do this instead and keep the ordering (sequencing)? Like so:</p>
  
  <pre><code>var variable1 = 'test1.txt'
  var variable2 = 'test2.txt'
  var return_value = do_something(variable1);
  var return_another_value = do_something_else(return_value); &lt;--sync
  writeData(return_another_value); &lt;-- async function
  var final_value = do_something_last(variable2);  &lt;-- sync function
  // could potentially have async calls again after a sync call
  writeDataAgain(return_another_value); &lt;-- async function
  </code></pre>
  
  <h2>Clarifications</h2>
  
  <p>What I thought was since some of these sync functions are going to need to be fired after the async, I needed to make them Promise aware in order to keep the sequence straight, like so:</p>
  
  <blockquote>
    <p>sync functions made promise aware</p>
  </blockquote>
  
  <pre><code>do_something = function (input) {
    var deferred = Q.defer();
    // assign variables, do other sync stuff 
    deferred.resolve(_output_result_);
    return deferred.promise;
  }
  do_something_else = function (input) {
    var deferred = Q.defer();
    // assign variables, do other sync stuff 
    deferred.resolve(_output_result_);
    return deferred.promise;
  }
  do_something_last = function (input) {
    var deferred = Q.defer();
    // assign variables, do other sync stuff 
    deferred.resolve('completed workflow');
    return deferred.promise;
  }
  </code></pre>
  
  <p>this would allow me to do this:</p>
  
  <pre><code>do_something(variable)
  .then(do_something_else) &lt;-- these need to execute before writeData
  .then(writeData) &lt;-- a async node fs call to writeFile
  .then(do_something_last) &lt;-- I need this to happen after the writeDate
  .done();
  </code></pre>
  
  <p>After the feedback i've read, i guess what it seems like i'm really asking is:</p>
  
  <blockquote>
    <p>How do I create a function workflow, mixing non-promise sync and
    promise-aware async function calls, all the while keeping the ordering
    (or sequencing) of execution?</p>
  </blockquote>