title: Waiting for a forEach to finish before return from my promise / function
body: <p>I am using Firebase Cloud Firestore, however, I think this may be more of a JavaScript asynchronous vs synchronous promise return issue.</p>
  
  <p>I am doing a query to get IDs from one collection, then I am looping over the results of that query to lookup individual records from another collection based on that ID. </p>
  
  <p>Then I want to store each found record into an array and then return the entire array. </p>
  
  <p><code>results.length</code> is always 0 because <code>return results</code> fires before the forEach completes. If I print <code>results.length</code> from inside the forEach it has data.</p>
  
  <p>How can I wait until the forEach is done before returning from the outer promise and the outer function itself?</p>
  
  <pre><code>         getFacultyFavoritesFirebase() {
              var dbRef = db.collection("users").doc(global.user_id).collection("favorites");
              var dbQuery = dbRef.where("type", "==", "faculty");
              var dbPromise = dbQuery.get();
              var results = [];
              return dbPromise.then(function(querySnapshot) {
                  querySnapshot.forEach(function(doc) {
                    var docRef = db.collection("faculty").doc(doc.id);
                    docRef.get().then(function(doc) {
                      if (doc.exists) {
                          results.push(doc);
                      }
                    })
                  });
                  console.log(results.length);
                  return results;
              })
              .catch(function(error) {
                  console.log("Error getting documents: ", error);
              });
            }
  </code></pre>