Issue
I have to work with AngularJS 1.5.5.
I am calling multiple Rest-Services and need to work with all the results at the same time.
$scope.callWebservices = function(){
let promises = {
first: callFirstWebservice(),
second: callSecondWebservice()
}
$q.all(promises).then(function(results){
console.log(results.first);
console.log(results.second);
console.log(results.second.value);
});
}
function callFirstWebservice(){
return myService.get({
myParameter : $scope.myId
}, function(res){
}, function(err){
console.log(err);
});
}
function callSecondWebservice(){
return myService.get({
myParameter : $scope.myId
}, function(res){
}, function(err){
console.log(err);
});
}
If I call callWebservices
the chrome dev-console logs two unresolved promises like this: m {$promise: d, $resolved: false}
and the third log is a TypeError, because result.second
is undefined.
When I expand the logoutputs of the promises it refreshes the state and shows $resolved: true
.
Shouldn't .then()
wait for all promises to be resolved, before executing?
Am I missing something?
EDIT: Corrected a typo (result != results) that is not present in the original code.
Solution
I found the problem.
The methods callFirstWebservice and callSecondWebservice
seem to return a $resource instance.
I had to add .$promise
to the returnvalue as stated in the angular docs for $resource:
The Resource instances and collections have these additional properties: $promise: the promise of the original server interaction that created this instance or collection.
After changing these to
function callFirstWebservice(){
return myService.get({
myParameter : $scope.myId
}, function(res){
}, function(err){
console.log(err);
}).$promise;
}
function callSecondWebservice(){
return myService.get({
myParameter : $scope.myId
}, function(res){
}, function(err){
console.log(err);
}).$promise;
}
everything is working as intended!
Answered By - 1stkeks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.