title: Wait for multiple Promises in Firebase Functions
body: <p>I try to figure out how I can wait for several promises to be executed before the function ends.</p>
  
  <p>Essentially, this is what I try to do:</p>
  
  <ul>
  <li>if a user deletes his/her account, I want to clean up all data which is associated with him</li>
  <li>direct data can be deleted</li>
  <li>if a user is part of a group, the group shall still exists, if other users are in that group; otherwise the group shall be deleted as well</li>
  </ul>
  
  <p>Here is what I tried so far:</p>
  
  <p>A) Main function (starts the first level of promises): </p>
  
  <pre><code>export function cleanUpAllData(user) {
    const userId = user.uid;
    const promises = [];
    promises.push(deleteCategoriesData(userId)); // this works fine
    promises.push(deleteUserAndGroupData(userId)); // this one has other promises which still run when Promise.all() is finished
    Promise.all(promises)
      .then(() =&gt; {
        return "ok"; // this works so far, but not all promises are resolved
      })
      .catch(errPromises =&gt; {
        console.log("an error occured during the processing of main promises");
        console.log(errPromises, errPromises.code);
        return "error";
      })
  }
  </code></pre>
  
  <p>B) deleteUserAndGroupData function (the other promise is working fine): each group found in the user data starts another level of promises and also triggers a thrid level of promises (deleteGroup) - the rest is working fine</p>
  
  <pre><code>function deleteUserAndGroupData(userId) {
    const promisesUserData = [];
  
    return admin.firestore().collection('users').doc(userId).collection('groups').get()
      .then(userGroups =&gt; {
          userGroups.forEach(userGroupData =&gt; {
              // delete group data
              promisesUserData.push(deleteGroups(userId, userGroupData.id)); // here are other promises that need to be resolved - somewhere is a problem              
              // delete all Groups in Users (subcollection)
              promisesUserData.push(deleteGroupInUser(userId, userGroupData.id)); // this works fine          
          });
          Promise.all(promisesUserData)
          .then(() =&gt; {
              admin.firestore().collection('users').doc(userId).delete()
                  .then(() =&gt; {
                      return "user data deleted"; // works fine
                  })
                  .catch(() =&gt; {
                      console.log("an error occured during deleting of user");
                      return "error";
                  });
          })
          .catch(errPromises =&gt; {
              console.log("an error occured during the processing of promisesUserData");
              console.log(errPromises, errPromises.code);
              return "error";
          })
      })
      .catch(errUserGroups =&gt; {
          console.log(errUserGroups, errUserGroups.code);
          return "no groups in User";
      });
  }
  </code></pre>
  
  <p>C) deleteGroups functions: deletes the sub collections in the group (works fine) and after that the group shall be deleted if there is no other user (which does not work)</p>
  
  <pre><code>function deleteGroups(userId,groupId) {
    const promisesDeleteGroups = [];
    // delete groups subcollection data 
    promisesDeleteGroups.push(deleteGroupRole(userId, groupId));
    promisesDeleteGroups.push(deleteGroupUser(userId, groupId));
  
    return Promise.all(promisesDeleteGroups).then(() =&gt; {
      checkForOthers(groupId);
    }).catch(errDeleteGroupSubcollections =&gt; {
      console.log("an error occured during the processing of promisesDeleteGroups");
      console.log(errDeleteGroupSubcollections, errDeleteGroupSubcollections.code);
      return "error";
    });
  }
  </code></pre>
  
  <p>D) checkForOthers function - checks if there is any entry in the subcollection and shall start to delete the group (but does not)</p>
  
  <pre><code>function checkForOthers(groupId) {
    return admin.firestore().collection('groups').doc(groupId).collection('users').get()
      .then(groupUsers =&gt; {
          return "other users exist - group should not be deleted";
      })
      .catch(errGroupUsers =&gt; {
          // console.log("no other group members - group can be deleted");
          // return "no other group members - group can be deleted";
          console.log(errGroupUsers, errGroupUsers.code);
          checkForInvitesAndDelete(groupId);
      });;
  }
  </code></pre>
  
  <p>E) checkForInvitesAndDelete: first I want to delete another subcollection which might or might not exists, if another user has been invited to the group; then it should trigger the final group delete (which seems not to work)</p>
  
  <pre><code>function checkForInvitesAndDelete(groupId) {
    const promisesInvitesAndDelete = [];
    return admin.firestore().collection('groups').doc(groupId).collection('invitations').get()
      .then(groupInvitations =&gt; {
          console.log("delete open Group Invitations");
          groupInvitations.forEach(groupInvite =&gt; {
              promisesInvitesAndDelete.push(deleteGroupInvite(groupId, groupInvite.id));
          });
          Promise.all(promisesInvitesAndDelete)
              .then(() =&gt; {
                  deleteGroup(groupId)
              })
              .catch(errPromisesInvitesAndDelete =&gt; {
                  console.log("an error occured during the processing of deleting group invites");
                  console.log(errPromisesInvitesAndDelete, errPromisesInvitesAndDelete.code);
                  return "error";
              });
      })
      .catch(() =&gt; {
          console.log("no open invitations");
          deleteGroup(groupId);
      });
  }
  </code></pre>
  
  <p>F) deleteGroup function</p>
  
  <pre><code>function deleteGroup(groupId) {
    return admin.firestore().collection('groups').doc(groupId).delete();
  }
  </code></pre>
  
  <p>I am relatively new to programming, especially Firebase Functions, so any help would be appreciated!!</p>
  
  <p>Thank you!</p>