title: Pub Sub Cloud Function - Async Await
body: <p>Trying to accomplish the following via a scheduled Firebase Function:</p>
  <ol>
  <li>Fetch Firestore Collection</li>
  <li>Delete Collection</li>
  <li>Make Get Request to External API</li>
  <li>Write Get Request results to Firestore Collection.</li>
  </ol>
  <p>The function is failing to deploy. Error logs only say <code>{&quot;code&quot;:3,&quot;message&quot;:&quot;Function failed on loading user code. This is likely due to a bug in the user code.</code></p>
  <p>When I run the emulator suite locally, I get no errors, but the DB does not update.</p>
  <p>I'm getting a linting error on the &quot;=&gt;&quot; that's attached to the &quot;onRun&quot; method. Not sure if this is a code problem or an ESLint problem for ES6 in Firebase Functions.</p>
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  const axios = require('axios');
  
  admin.initializeApp();
  const db = admin.firestore();
  
  exports.scheduledFunction = functions.pubsub
      .schedule(&quot;every 2 minutes&quot;)
      .timeZone(&quot;America/New_York&quot;)
      .onRun(async (context) =&gt; {
  
        try {
  
          const querySnapshot = await db.collection(&quot;my_collection&quot;).get();
          console.log(querySnapshot.docs)
          const promises = querySnapshot.docs.map(doc =&gt; db.collection(&quot;my_collection&quot;).doc(doc.id).delete());
  
          await Promise.all(promises);
  
          const options = {
            &quot;method&quot;: &quot;get&quot;,
            &quot;url&quot;: &quot;www.myUrl.com&quot;,
            &quot;headers&quot;: {
              &quot;Cookie&quot;: &quot;...&quot;,
            },
          };
  
          const axiosResponse = await axios(options);
          const apiResponse = JSON.parse(axiosResponse.data);
          const parsedResponse = apiResponse[&quot;news_results&quot;];
  
          const docCreationPromises = parsedResponse.map(response =&gt; db.collection(&quot;my_collection&quot;).add(parsedResponse))
  
          await Promise.all(docCreationPromises);
  
          return null;
  
        } catch (error) {
          console.log(error);
          return null;
        }
  
      });
  </code></pre>