I'm trying to wait on 3 promises but $q.all
appears to resolve them at once and returns undefined
for each single value, I can't figure out why:
this.doWork = function() {
var deferred = $q.defer();
var a = get('a'),
b = get('b'),
c = get('c');
$q.all([a.promise, b.promise, c.promise])
.then(function(values) {
deferred.resolve(new Test(values[0], values[1], values[2]));
}, function(reason) {
deferred.reject(reason);
});
return deferred.promise;
};
function get(param) {
var deferred = $q.defer();
$timeout(function() {
if (true) {
deferred.resolve({
value: param
});
} else {
deferred.reject({
message: "Really bad"
});
}
}, 1000);
return deferred.promise;
}
(in the actual code get()
uses $http
instead of $timeout
, of course). Here's a Plnkr with the code, can anybody please shed some light on what the issue is?
You should not be doing .promise
on promise
object returned by get
method, because you had already returned promise
form get
method.
$q.all([a, b, c])
When you're doing
a.promise
,b.promise
&c.promise
they all becomesundefined
& then$q.all
array become$q.all([undefined, undefined, undefined])
passing them to$q.all
will giveundefined
result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.