title: Wait till $http is finished in order to output the result in AngularJS
body: <p>How can I wait with my function till the $http request is finished?</p>
  
  <p>My <code>services.js</code> looks as follows:</p>
  
  <p>var app = angular.module('starter.services', []);</p>
  
  <pre><code>app.factory('Deals', function($http) {
      function getDeals() {
          $http.get('http://www.domain.com/library/fct.get_deals.php')
          .success(function (data) {
              var deals = data;
              return deals;
          })
          .error(function(err){
        });
    }
  
    return {
      all: function() {
          return getDeals();
      },
      get: function(keyID) {
          //...
      }
    }
  });
  </code></pre>
  
  <p>My <code>controllers.js</code> looks like:</p>
  
  <pre><code>var app = angular.module('starter.controllers', []);
  
  app.controller('DealCtrl', function($scope, Deals) {
      $scope.deals = Deals.all();
      console.log($scope.deals);
  });
  </code></pre>
  
  <p>The <code>console.log</code> in my <code>controllers.js</code> file outputs "undefined", but when I output the deals in the <code>getDeals()</code> function it contains the correct array which I get from my server.</p>
  
  <p>What am I doing wrong?</p>