You could use $q.all()
to execute all you requests in parallel, and only have the callback called when all the promises are fulfilled:
$q.all([myService.getData('something'), myService.getData('other_thing')]).
then(function(array) {
var somethingResponse = array[0];
var otherThingResponse = array[1];
...
});
If you need work done on each retrieved data, then you should do it in the service:
function getSomething() {
return getData('something').then(function(response) {
return transformResponse1(response);
});
}
function getOtherTthing() {
return getData('other_thing').then(function(response) {
return transformResponse2(response);
});
}
Indeed, then()
returns a new Promise which is resolved via the return value of the success callback.
And if you need to do something when all the transformed responses are available, then you can do it in the globall callback:
$q.all([myService.getSomething(), myService.getOtherThing()]).
then(function(array) {
var transformedResponse1 = array[0];
var transformedResponse2 = array[1];
// do what you need here
});