title: Node.js Understanding, Two Method Chaining
body: <p>I my understanding of Node.js promises is still very rough and I guess I am missing some crucial point here, but I just want to have to functions return a promise, so that I can chain them together and have something like that:</p>
  
  <pre><code>importer = new require(importers[i].module);
  importer.init(importers[i].options)
    .then(importer.run) // also it is possible to give some parameters here?
    .then(function (res) {
      events = res;
    });
  </code></pre>
  
  <p>The importer is simple module with two methods just like that:</p>
  
  <pre><code>'use strict';
  
  var q = require('q');
  var FB = require('fb');
  var promises = [];
  
  var FacebookImport = {
    init: function (options) {
      var deferred = q.defer();
  
      FB.api('oauth/access_token', {
        client_id: options.clientID,
        client_secret: options.clientSecret,
        grant_type: 'client_credentials'
      }, function (res) {
        if (!res || res.error) {
          console.log(!res ? 'error occurred' : res.error);
          return;
        }
  
        var accessToken = res.access_token;
        FB.setAccessToken(res.access_token);
        deferred.resolve(accessToken);
      });
  
      return deferred.promise;
    },
    run: function () {
      var deferred = q.defer();
  
      FB.api('4', function (res) {
        if (!res || res.error) {
          console.log(!res ? 'error occurred' : res.error);
          return;
        }
        console.log(res);
        deferred.resolve(res);
      });
  
      return deferred.promise;
    }
  };
  
  exports = module.exports = FacebookImport;
  </code></pre>
  
  <p>The whole point is to first get the authentication token from Facebook and the query the graph api for some stuff. So these two functions should only run after one another.</p>
  
  <p>Thanks,
  Michael</p>