title: angular.forEach and objects
body: <p><strong>The Problem:</strong></p>
  
  <p>I'm doing (what I think is but maybe isn't) a simple <code>angular.forEach</code> over an array then using <code>$resource</code> to make a call based on each value returned. The result of each call is an object, as I'm expecting. But, I can't get these objects to join harmoniously in the way the <a href="http://docs.angularjs.org/api/angular.forEach">angular.forEach documentation</a> demonstrates. </p>
  
  <p>But first, some code that works and then a look at code that fails.</p>
  
  <p><strong>Works</strong>:</p>
  
  <pre><code>var uniqueIds = {};
  angular.forEach(object, function(key, value){
      this.push(key.uniqueIds);
  }, uniqueIds);
  console.log(uniqueIds)
  // uniqueIds equals the expected array
  </code></pre>
  
  <p><strong>Fails</strong></p>
  
  <p>Here's where things get tricky. Now, in the next sample, inside the <code>angular.forEach</code> is a <code>$resource</code> call. </p>
  
  <pre><code>angular.forEach(array, function(index){
      Resource.query({id:index}, function(result){
          console.log(result)
          // returns the expected object, 
          // but, as expected, I can't access the object outside the async call
      });
      console.log(result);
      // result is undefined
   });
  </code></pre>
  
  <p>Given the async-ness, it seems a promise could solve the problem. But it doesn't--I'm really still inside the async call. Nor does assigning <code>result</code> to <code>$scope</code> work. In short, I can't seem to get a value outside the <code>Resource.query</code>. </p>
  
  <p><strong>What Do I Need To Happen?</strong></p>
  
  <p>I need the returned object of each <code>$resource</code> call to add up to one object (using angular.extend? I've tried that) in the same way the <code>angular.forEach</code> created an array. I've tried many different approaches, most all based on answers for general async questions asked here, but so far to no avail. I'm positive it's an issue with getting the value out of the <code>$resource</code> call, but how to do that in this case I'm a bit baffled.</p>