I'm trying to write a function that sends multiple requests to the server based on the number of Ids in the idArray
. The problem I'm facing is that the data that is pushed into the dataArray
does not follow the proper sequence of the corresponding Ids of the idArray
. I tried adding the timeout
to the HTTP requests so that the previous request is fully processed before the next iteration of the for
loop, but that too does not seem to work. Please help.
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;
}
}
}