title: Promise inside foreach loop in typescript, construct array from response and wait for all done
body: <p>Im trying to iterate over an suites array that returned from http call then on each iteration have to call api to get points against each suite. Each call returned a promise and i have to construct an array and populate result from each api call response. I would like to return result array once everything within the suites.forEach loop is all done. But the current code retrun control after 1st suites.forEach occurance.</p>
  <pre><code>import Q = require(&quot;q&quot;);
  public static getTestResults(testcaseId: number): Promise&lt;TestResult[]&gt;{
      
      let deferredPromise = Q.defer&lt;TestResult[]&gt;();
      let results: TestResult[] = [];
  
      let testManagementRestClient: TestManagementRestClient.TestHttpClient4_1;
      testManagementRestClient = TestManagementRestClient.getClient();
                   
        testManagementRestClient.getSuitesByTestCaseId(testcaseId).then(suites =&gt; {
              suites.forEach(suite =&gt; {
                  testManagementRestClient.getPoints(suite.project.id, +suite.plan.id,
                                                      suite.id, undefined, undefined,
                                                      testcaseId.toString(), undefined,
                                                      true, undefined, undefined
                          ).then(points =&gt; {
                              points.forEach(point =&gt; {                            
                                      results.push(
                                          {
                                              projectId: suite.project.id,
                                              runId: point.lastTestRun.id,
                                              configuration: point.configuration.name
                                          }
                                      );
                              });
                              deferredPromise.resolve(results);
                          });  
              });
          });
      return deferredPromise.promise;
  }
  </code></pre>
  <p>I googled and find out the $q.all() but didn't know how i can implement this on this specific scenario and if it resolve my issue.</p>
  <p>Any advices would be welcomed! Thanks!</p>