title: AngularJS promise after foreach
body: <p>I have a problem with my promise after a foreach,</p>
  <p>I would like to remove the object which has no parent from my database.</p>
  <p>I read this <a href="https://stackoverflow.com/q/35201724/5453732">post</a> and I tried many things, for example this:</p>
  <p><strong>controller</strong></p>
  <pre><code>project.getAll().then(function(results) {
              return $scope.array_projects = results;
          }).then(function(array_projects) {
              $scope.results = [];
              console.log(array_projects);
  
              angular.forEach(array_projects, function(result) {
                  console.log(result);
  
                  var project = new Project(result);
  
                  var promise = project.getOne(result.ParentId);
                  $scope.result.push(promise);
                  promise.then(function(results) {
                      return results.length ? project:null;
                  });
                  
                  // if(result.ParentId !== null) {
                  //     console.log('ParentId');
                  //     var promise = project.getOne(result.ParentId).then(function(results) {
                  //         //console.log(results);
                  //
                  //         if (results.length === 0){
                  //             console.log('remove children');
                  //         } else {
                  //             $scope.results.push(project);
                  //             console.log('parent exist, children added!');
                  //
                  //         }
                  //
                  //     });
                  //
                  // }
  
              });
  
              // after forEach
              $q.all($scope.results).then(function(results){
                  console.log($scope.results);
                  // result is an array of mixed Project object or null value, just remove the null value and you're fine :)
                  var trueResults = [];
                  for(var i = 0; i &lt; results.length; i++){
                      if(results[i]){
                          trueResults.push(results[i]);
                      }
                  }
                  // now use trueResults
                  console.log(trueResults);
              })
  
          });
  </code></pre>
  <p><strong>model</strong></p>
  <pre><code>var Project = function (properties) {
          // Model
          this.description = null;
          this.file        = null;
          this.name        = null;
          this.ParentId    = null;
          this.path        = null;
  
          angular.extend(this, properties);
      };
  
  // With service
  // ------------------------------------------------------------------------------------------------------
  // ------------------------------------------------------------------------------------------------------
  
  Project.prototype.getAll = function () {
      return ProjectService.getAll();
  };
  
  Project.prototype.getOne = function (id) {
      return ProjectService.getOne(id);
  };
  </code></pre>
  <p><strong>project.service.js</strong></p>
  <pre><code>this.getAll = function (params) {
      var projects = [];
      return db.selectAll('projects').then(function(results) {
          for(var i=0; i &lt; results.rows.length; i++){
              projects.push(results.rows.item(i));
          }
          return projects;
      });
  };
  
  this.getOne = function (id) {
      var project = [];
      console.log(id);
      return db.select('projects', {
          &quot;id&quot;: id
      }).then(function(results) {
          for(i=0; i &lt; results.rows.length; i++){
              project.push(results.rows.item(i));
          }
          return project;
      })
  };
  </code></pre>
  <p>But my console.log($scope.results); show '[]'</p>
  <p>In my BDD I have :</p>
  <pre><code>id/id/ParentId/name/file/date
  
  5   5       test    crm-site.net    /release.txt    2016-04-26 08:43:17
  
  6   6   5   test2   crm-site.net    /release.txt    2016-04-26 08:43:19
  
  7   7       test3   crm-site.net    /release.txt    2016-04-26 08:43:20
  
  8   8   7   test4   crm-site.net    /release.txt    2016-04-26 08:43:21
  </code></pre>