title: Wait for promises inside of a angular.forEach loop
body: <p>I know this has been asked quite a few times already but after a day of search I still don't get it to work, although it's just like what is shown as a solution everywhere...</p>
  
  <p>I have a async request to a database which returns an array of data. For each object in this array I need to start another async request to the database and as soon as ALL of these async requests resolve, I want to return them. I read you could do it with $q.all(...)</p>
  
  <p>So here's the code:</p>
  
  <pre><code>Factory.firstAsyncRequest(id).then(function (arrayWithObjects) {
  var promises = [];
  var dataArr = [];
    angular.forEach(arrayWithObjects, function (object, key) {
       var deferred = $q.defer();
       promises.push(deferred);
       Factory.otherAsyncRequest(key).then(function (objectData) {
          dataArr.push({
             name: objectData.name,
             key: key,
             status: objectData.status
          });
          deferred.resolve();
          console.info('Object ' + key + ' resolved');
       });
    });
    $q.all(promises).then(function () {
       $rootScope.data = dataArr;
       console.info('All resolved');
    });});
  </code></pre>
  
  <p>From the console I see that the $q.all is resolved BEFORE each object. Did I get something wrong? This seems to work for everyone...</p>
  
  <p>Your help is highly appreciated, been looking the whole night, it's 5:30am now lol..</p>
  
  <p>Cheers</p>
  
  <p>EDIT:
  So for anyone who's coming here later: It was just the promises.push(deferred.PROMISE) bit. Tho, I read that anguar.forEach is actually not a recommended method to loop through array because it was originally not constructed to be used by the end-user. Don't know if that's correct but I figured out another way if you don't want to use angular.forEach:</p>
  
  <pre><code>Users.getAll(uid).then(function (users) {
            var uids = ObjHandler.getKeys(users); //own function just iterating through Object.keys and pushing them to the array
            var cntr = 0;
            function next() {
              if (cntr &lt; uids.length) {
                Users.getProfile(uids[cntr]).then(function (profile) {
                  var Profile = {
                    name: profile.name,
                    key: uids[cntr],
                    status: profile.status
                  });
                  dataArr[uids[cntr]] = Profile;
                  if(cntr===uids.length-1) {
                    defer.resolve(); 
                    console.info('Service: query finished');
                  } else {cntr++;next}
                });
              }
            }
            next();
          });
  </code></pre>
  
  <p>And the getKey function:</p>
  
  <pre><code>.factory('ObjHandler', [
  function () {
    return {
      getKeys: function(obj) {
        var r = [];
        for (var k in obj) {
          if (!obj.hasOwnProperty(k))
            continue;
          r.push(k)
        }
        return r
      }
    };
  }])
  </code></pre>