title: $scope.$apply in AngularJS - Function executed each time is the last defined function
body: <p>If I call the code below inside a loop of asynchronous HTTP requests I get latter response. Any suggestions on where I could be going wrong?</p>
  
  <p>NOTE: This is essentially pseudo code.</p>
  
  <pre><code>function successful_request(site) {
      console.log('In: ' + site.id);
      $scope.$apply(function() { console.log('Out: ' + site.id); }
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>In: 1
  In: 2
  In: 3
  Out: 3
  Out: 3
  Out: 3
  </code></pre>
  
  <p>I hope I've made sense here. I suspect it's a case of how I'm calling $scope.$apply but I'm not sure what I should do differently.</p>