I\'m working on an $http call that loops over each of multiple api\'s and returns all of the data in one object. I usually have the promise ready to resolve when the $http c
$q.all
and a map
function are what you need here:
function getSearchData() {
return {
// returns a promise for an object like:
// { abo: resultFromAbo, ser: resultFromSer, ... }
loadDataFromUrls: function () {
var apiList = ["abo", "ser", "vol", "con", "giv", "blo", "par"];
return $q.all(apiList.map(function (item) {
return $http({
method: 'GET',
url: '/api/' + item
});
}))
.then(function (results) {
var resultObj = {};
results.forEach(function (val, i) {
resultObj[apiList[i]] = val.data;
});
return resultObj;
});
}
};
}