Reputation: 10932
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?
Upvotes: 3
Views: 1925
Reputation: 136184
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.
Upvotes: 7