title: Chain promises with AngularJS
body: <p>I have a service called paymentStrategy that get injected in my controller.</p>
  
  <pre><code>$scope.buy = function() {
    paymentStrategy.buy()
      .then(function(response) {
  
    }
  }
  </code></pre>
  
  <p>This buy method from paymentStrategy triggers several methods that needs to be called sequentially. When all the methods within buy() are done, then() needs to be called.</p>
  
  <p>It is probably trivial but I am quite new to angular.</p>
  
  <p>At the moment, buy().then() gets triggered straight after the init() methods.
  I have the feeling we need to put all theses methods in a array of promises and apply $q.all(). </p>
  
  <p>Any help or suggestion would be greatly appreciated</p>
  
  <pre><code>angular.module('deps-app.payment.services', []).
    factory('paymentStrategy', function($q) {
  
   var deferred = $q.defer();
   var ITEM_TO_PURCHASE = "test.beer.managed";
   var promises = [];
  
   var handlerSuccess = function(result) {
        deferred.resolve(result);
    };
  
   var handlerError = function(result) {
        deferred.reject(result);
    };
  
   _init = function() {
  
       inappbilling.init(handlerSuccess, handlerError, { showLog:true }); 
       return deferred.promise;
      }
  
    _purchase = function() {
          inappbilling.buy(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
          return deferred.promise;
    }
  
    _consume = function() {
          inappbilling.consumePurchase(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
          return deferred.promise;
    }
  
  return  {
  
       buy: function() {
  
        _init();
          .then(_purchase());
          .then(_consume());  
  
        return deferred.promise;                    
      }
  
   }
  });
  </code></pre>