Reputation: 9664
I have set up a promise with Angular using the $q
service.
var get = function() {
var defer = $q.defer();
setTimeout(function() {
defer.resolve('test');
}, 1000);
return defer.promise;
}
When I call this function, it returns the expected data like so:
var promise1 = api.get().then(function(data){
console.log(data); //logs test
});
var promise2 = api.get().then(function(data){
console.log(data); //logs test
});
But when I use the all
method, I get an array containing undefined
twice.
$q.all([promise1, promise2]).then(function(data){
console.log(data); //logs [undefined, undefined];
});
Am I missing something here?
Upvotes: 3
Views: 1324
Reputation: 913
It must like this:
var promise1 = api.get();
var promise2 = api.get();
Then, types of promise1 and promise2 are "promise" and you can use them in $q.all().
$q.all([promise1, promise2]).then(function(data){
console.log(data); //logs ['test', 'test'];
});
Upvotes: 1
Reputation: 25352
You have to return data from callback
Try like this
var promise1 = function(){
return api.get().then(function(data){
return data;
});
}
Upvotes: 6