Skip to content Skip to sidebar Skip to footer

Is There A Second Success In Angular $q.all() As In Jquery $.get()

Looking at the jQuery documentation I found out the following: $.get( 'example.php', function() { alert( 'success' ); }) .done(function() { alert( 'second success' ); <-

Solution 1:

Why couldn't you do that :

$q.all([
        $http.get(ROOT + "Lookup/GetStates"),
        $http.get(ROOT + "Lookup/GetCountries"),
        $http.get(ROOT + "Address/GetAddresses"),
    ]).then(function (results) {
        $scope.states = jQuery.parseJSON(results[0].data.data);
        $scope.country = jQuery.parseJSON(results[1].data.data);
        $scope.addresses = jQuery.parseJSON(results[2].data);
        $scope.setupControls();
    });

I think there is no need for a second success. Keep It Simple (and Sexy?)

Solution 2:

Have a look at this:

.finally(function() {
  // do this on both error and success
});

As it says, executes after success and error

So Full code:

$q.all([
        $http.get(ROOT + "Lookup/GetStates"),
        $http.get(ROOT + "Lookup/GetCountries"),
        $http.get(ROOT + "Address/GetAddresses"),
    ]).then(function (results) {
        $scope.states = jQuery.parseJSON(results[0].data.data);
        $scope.country = jQuery.parseJSON(results[1].data.data);
        $scope.addresses = jQuery.parseJSON(results[3].data);
    }).finally(function() { /* <-- here */// do this on both error and success
    });

Solution 3:

Angular's $q provide the same methods as jQuery's. Details here.

One approach I could think of is the following:

$http.get('parent request')
.then(function(win){
      // do something when everything is resolved
   },
   function(err){
      //handle error
   },
   function(progress){
      //do something when a child request is resolved
   }
);

... where the "parent request" makes some child requests & for each child resolved request you can use .notfiy() method to update the parent request & also you should keep track of all your child request status and when all are resolved you can call .resolve() and do whatever you want ($scope.setupControls() in your case).

Solution 4:

You should be able to invoke then multiple times.

https://docs.angularjs.org/api/ng/service/$q

Chaining promises Because calling the then method of a promise returns a new derived promise, it is easily possible to create a chain of promises:

promiseB = promiseA.then(function(result) {
   return result + 1;
});

// promiseB will be resolved immediately after promiseA is resolved and its value// will be the result of promiseA incremented by 1$q.all([
        $http.get(ROOT + "Lookup/GetStates"),
        $http.get(ROOT + "Lookup/GetCountries"),
        $http.get(ROOT + "Address/GetAddresses"),
    ]).then(function (results) {
        $scope.states = jQuery.parseJSON(results[0].data.data);
        $scope.country = jQuery.parseJSON(results[1].data.data);
        $scope.addresses = jQuery.parseJSON(results[3].data);
    }).then(function (result) {
         //do something here
    }) ;

Post a Comment for "Is There A Second Success In Angular $q.all() As In Jquery $.get()"