Working on a sandbox app to learn angular.js I have encountered the following pattern in several places in my code. I find myself having to query the mongoDB in a loop. As I understand it, each call happens in its own async task. How do I know when all the tasks are completed?
For instance, I have an array of states. Often I need to set someProperty to someNewValue for each of the states. Once all the states have been updated I would like to call someFunction().
for (var i = 0; i < $scope.states.length; i++) {
$scope.states[i].someProperty = someNewValue;
$scope.states[i].$update({stateId: $scope.states[i].id}, function() {
someFunction();
});
}
For now, the only way I can think of doing this is to call someFunction() every time each update succeeds. I know there must be a smarter and better way of doing this.
What would you be your approach?