I have a dilemma with my services - they both return the same type of objects but with different properties based on a flag. These services utilize functions that return promises.
Service 1 - fetchLocations:
this.fetchLocations = function () {
return $http.get('http://somewhere.com').then(function (response) {
return response;
}).catch(exception.catcher('Something went wrong'));
};
Service 2 - retrieveSpots:
this.retrieveSpots = function () {
return $http.get('http://somewhere.com/').then(function (response) {
return response;
}).catch(exception.catcher('Something went wrong'));
};
Now, I have a master Service that orchestrates these two services. The challenge is to ensure that the wrapper function doesn't return the combined results until all promises are fulfilled and have returned valid information (or null). My current implementation looks like this:
this.getCombinedResults = function () {
var combined= [];
var apiCalls = [service1.fetchLocations(), service2.retrieveSpots()];
$q.all(apiCalls.map(function(call) {
return call;
})).then(function (response) {
angular.forEach(response, function (item, index) {
combined.push(item);
});
});
return combined;
};
I suspect there might be an issue with how I'm handling multiple promises in the wrapper service. When I invoke the wrapper function, it returns null. I need to investigate this further, but any insights on whether my approach is correct would be greatly appreciated. Thank you.