title: Split cloud function in helper function and async await
body: <p>I have a http triggered Google cloud function that returns some stats for a given user.
  Since other cloud functions should use the stats fetching from the firestore database as well, I want to split it into a helper function. It looks like this:</p>
  
  <pre><code>async function getStatsForUser(userId: string) {
      console.log(userId); // printed properly
      const stats = {};
  
      const docs = await admin.firestore().collection('stats')
          .where('user', '==', userId)
          .orderBy('key', 'desc').limit(1).get();
  
      docs.forEach(doc =&gt; {
          stats["key"] = doc.data().value;
          console.log("stats", stats); // this is never printed
          return stats;
      });
  }
  </code></pre>
  
  <p>And the http cloud function (basically just for testing at the moment) like this:</p>
  
  <pre><code>export const getStats = functions.https.onRequest(async (request, response) =&gt; {
  
      const userId = request.query.userId;
  
      try {
          const stats = await getStatsForUser(userId);
          console.log("stats2", stats); // printed but empty
          response.send(stats); // nothing is received on client site
  
      } catch (error) {
          console.log(error);
          response.status(500).send(error);
      }
  });
  </code></pre>
  
  <p>So the log output looks like this:</p>
  
  <pre><code>userId abcdef...
  stats2
  </code></pre>
  
  <p>So somehow the cloud function is returning before awaiting the async helper. 
  I know I have some problems with the async/await's but I cant get it to work. What am I doing wrong?</p>