Q:

how to check multiple responses simultanously

$scope.placeOrder = function() {

var callbackBalance = apiService.updateBalance($scope.order);

callbackBalance.then(function(data) {

    if(data.data.success) {

      var callback = apiService.createOrder($scope.order);
      callback.then(function(data){
            if(data.data.success)
            {
              localStorageService.cookie.clearAll();
              alert("Order Placed  Successfully");
              $state.go("createOrder");
            }
            else
              alert('Sorry! Cannot place order');
      });

    }
    else
       {
         alert('Cannot update company balance')
      }

  });

This a code to place order for a company and update its balance amount according to the order total.The code works fine but according to this code first the balance amount API is called and once its response is success order API will be called and its response will be checked.But, how can we check if both are successful then only update databases for balance and order.Right now a case can be that balance is updated for a company but for some reason no order was placed.
I am using MEAN stack for my development.  

A:

You can use $q service from angular

A service that helps you run functions asynchronously, and use their
  return values (or exceptions) when they are done processing. You can
  create a request object like this

You can create request array and pass it to $a.all function.
var request = [apiService.updateBalance($scope.order), apiService.createOrder($scope.order)]

and use $q.all function to get provided request simultaneously
 $q.all(request).then(function (response) {

}, function (error) {

});

that will get the request data simultaneously. Make sure to add $q as dependency.

