I am struggling to create a function that sends multiple requests to the server based on the number of Ids in the idArray
. The issue I am encountering is that the data being pushed into the dataArray
does not align with the corresponding Ids of the idArray
. I attempted to use a timeout
for the HTTP requests in hopes of ensuring one request is fully processed before moving onto the next iteration of the for
loop, but unfortunately, this approach did not yield the desired results. Any assistance would be greatly appreciated.
function commonService($http, $q) {
return {
getAboutContent: function() {
var dataArray = [];
var deferred = $q.defer();
var idArray = ['about2', 'about3'];
var count = 0;
angular.forEach(idArray, function(id) {
$http.get('server url/' + id).success(function(data) {
dataArray.push(data);
count++;
if (count == idArray.length) {
deferred.resolve(dataArray);
}
}).error(function(error) {
console.log('error', error);
deferred.reject(error);
});
});
return deferred.promise;
}
}
}