user11130182
user11130182

Reputation: 121

Angularjs Chaining Promises and $q

while executing the code alert("promiseC done") is displaying first and then alert("promiseC"); is displaying last because of this the data was not displaying properly from function Displaydata(); from belo code.

App.controller("Ctrl", function ($scope, $http, $location, $window, $sce, $q) {

    var promiseA = getMA(Id).then(function () {
    alert("GOT MA data from API call");
    // doing some stuff with MA data
      ...
    }
    var promiseB = getMB(Id).then(function () {
    alert("GOT MB data from API call");
    // doing some stuff with MBdata
      ...
    }
    var promiseC = getMC(Id).then(function () {
    alert("GOT MC data from API call");
    // doing some stuff with MC data
      ...
      getotherdata(MC.Id).then(function () {
      ..
      }
     ..
      alert("promiseC");
   }


   var promiseB = promiseA.then(function (result) {
            alert("promiseA done");
    });
    var promiseC = promiseB.then(function (result) {      
        alert(" promiseB done");      
    });

    var promiseD = promiseC.then(function (result) {
            alert("promiseC done");
            Displaydata(); 
    });
}

How can i make to work properly so that alert("promiseC") will display first and then alert("promiseC done").

Also tried in below way but same issue as above in chain promises.

    var promiseA = function(){
     var deferedA = $q.defer(); 
        deferedA.resolve();

       return deferedA.promise;
    }
    var promiseB = function(){
    var deferedB = $q.defer(); 
        deferedB.resolve();

       return deferedB.promise;
    }
    var promiseC = function(){
  var deferedC = $q.defer(); 
  alert("promise c");
    deferedC.resolve();

   return deferedC.promise;
}
$q.all([promiseA, promiseB, promiseC]).then(function () { 
  alert("all done");
  Displaydata();
})

Upvotes: 1

Views: 38

Answers (0)

Related Questions