title: How to handle multiple async tasks in angular.js?
body: <p>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?</p>
  
  <p>For instance, I have an array of <em>states</em>. Often I need to set <em>someProperty</em> to <em>someNewValue</em> for each of the states. Once all the states have been updated I would like to call <em>someFunction()</em>.</p>
  
  <pre><code>for (var i = 0; i &lt; $scope.states.length; i++) {
      $scope.states[i].someProperty = someNewValue;
      $scope.states[i].$update({stateId: $scope.states[i].id}, function() {
          someFunction();
      });
  }
  </code></pre>
  
  <p>For now, the only way I can think of doing this is to call <em>someFunction()</em> every time each <em>update</em> succeeds. I know there must be a smarter and better way of doing this. </p>
  
  <p>What would you be your approach?</p>