title: Execute 2 functions one after the other in AngularJS
body: <p>I have two functions which provide data to my page in AngularJS.
  Because they provide data, I execute them like below <code>ng-init = "function1(); function2();"</code></p>
  
  <p>This all works great no problem. But because these functions don't finish at the same time, I have some part of my site loaded and another not loaded.</p>
  
  <p>I would like to execute them sequentially, once function 1 returns success or error the  execute function 2. Both functions simple fetch data using <code>$http</code> and return a JSON which I add to an empty <code>$scope</code>.</p>
  
  <p>Functions below;</p>
  
  <pre><code>$scope.getRestOpen = function () {
      $http({
          method: 'post',
          url: "http://www.example.co.uk/php/x/open-get.php",
          data: $.param({ 'location' : $scope.l, 
                         'time' : $scope.t,
                         'day' : $scope.d,
                         'type' : 'get_restopen' }),
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      }).
      success (function(data, status, headers, config){
          if(data.success &amp;&amp; !angular.isUndefined(data.data) ){
              $scope.open = data.data;
          } else {
              $scope.open = [];
          }
      }).
      error(function(data, status, headers, config) {
          //$scope.messageFailure(data.message);
      });
  }
  
  $scope.getRestClosed = function () {
      $http({
          method: 'post',
          url: "http://www.ecample.co.uk/php/x/closed-get.php",
          data: $.param({ 'location' : $scope.l, 
                         'time' : $scope.t,
                         'day' : $scope.d,
                         'type' : 'get_restclosed' }),
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      }).
      success (function(data, status, headers, config){
          if(data.success &amp;&amp; !angular.isUndefined(data.data) ){
              $scope.closed = data.data;
          } else {
              $scope.closed = [];
          }
      }).
      error(function(data, status, headers, config) {
          //$scope.messageFailure(data.message);
      });
  }
  </code></pre>
  
  <p>Cheers!</p>