在执行代码时,首先显示alert(“ promiseC done”),然后显示alert(“ promiseC”);由于这个原因,最后显示的是该数据未从函数Displaydata()中正确显示;来自belo代码。
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();
});
}
我如何使它正常工作,以便首先显示alert(“ promiseC”),然后再显示alert(“ promiseC done”)。
也尝试了以下方式,但在链承诺中遇到了与上述相同的问题。
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();
})