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 call has been made. Similar to this:
function getAllData(api) {
return $http({
method: 'GET',
url: '/api/' + api
})
.then(sendResponseData)
.catch (sendGetVolunteerError);
}
The current function I have loops over each api and pushes each object in the api into an array and then pushes it into an overall array. I had this functioning, returning an multi-dimensional array, which needed to be flattened out.
I'd like to return this in a promise, but am returning undefined
. Here is what I have so far? Is there a better way to approach this?
dataService:
function getSearchData() {
return {
loadDataFromUrls: function () {
var apiList = ["abo", "ser", "vol", "con", "giv", "blo", "par"];
var deferred = $q.defer();
var log = [];
angular.forEach(apiList, function (item, key) {
var logNew = [];
$http({
method: 'GET',
url: '/api/' + item
}).then(function (response) {
angular.forEach(response.data, function (item, key) {
this.push(item);
}, logNew);
return logNew;
});
this.push(logNew);
}, log);
$q.all(log).then(
function (results) {
deferred.resolve(
JSON.stringify(results))
},
function (errors) {
deferred.reject(errors);
},
function (updates) {
deferred.update(updates);
});
return deferred.promise;
}
};
};
Controller:
function getSearchData(){
return dataService.getSearchData.loadDataFromUrls;
}
$scope.searchData = getSearchData();
$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;
});
}
};
}
If you have an arbitrary set of api calls I would do something like this:
function getSearchData(){
var deferred = $q.defer();
var noOfCalls = apiList.length;
var results = [];
var called = 0;
angular.forEach(apiList, function(item, key) {
$http.get(url).then(function(result){
results.push(result);
called++;
if(called == noOfCalls){
deferred.resolve(results);
}
})
});
return deferred.promise;
}
However if you know what each api call represents its better to use $.all in this way
function search1(){
return $http.get(search1Url).then(function(result){
// do something to it
return result;
});
}
function search2(){
return $http.get(search2Url).then(function(result){
// do something to it
return result;
});
}
function search3(){
return $http.get(search3Url).then(function(result){
// do something to it
return result;
});
}
function search4(){
return $http.get(search4Url).then(function(result){
// do something to it
return result;
});
}
function getSearchResult(){
return $q.all([search1(), search2(), search3(), search4()]).then(function(results){
// OPTIONAL aggregate results before resolving
return results;
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With