I am trying to update multiple items within a foreach loop by sending HTTP requests, and I need a callback once all the requests are complete. Despite searching on Stack Overflow, I haven't found a solution that works for me.
Here is the snippet of my code:
$q.all(_($scope.students.items).each(function(item) {
$scope.student.update(); //this triggers an HTTP call
})).then(function() {
// I'm in need of a callback at this point
alert("complete"); //this should only be displayed after all students have been updated, but it appears before all network calls are finished
});
Below is a generic update function example:
self.update = function(item, callback) {
that.post(item, self.getUrl("update"), function(err, data) {
if (self.formatter) {
data = self.formatter(data);
}
callback(err, data);
});
};
Does anyone have any suggestions or solutions to this issue?