title: Slow, unreliable Firebase Cloud Functions
body: <p>In the app I'm building, users can request a word, and the app goes to the online Oxford Dictionaries API to get the definition, pronunciation, etc. I'm using Firebase Cloud Functions for the HTTP request, and writing the response to Cloud Firestore. Sometimes it works...slowly. Nine out of ten times the data isn't written to Firestore.</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  const request = require('request'); // node module to send HTTP requests
  
  admin.initializeApp();
  
  exports.oxford_English_US = functions.firestore.document('Users/{userID}/English_American/Word_Request').onUpdate((change, context) =&gt; {
  console.log(change.before.data());
  console.log(change.after.data());
  console.log(context.params.userID);
  if (change.after.data().word != undefined) {
    let options = {
      url: 'https://od-api.oxforddictionaries.com/api/v1/entries/en/' + change.after.data().word + '/pronunciations%3Bregions%3Dus',
      headers: {
        "Accept": "application/json",
        'app_id': 'TDK',
        'app_key': 'swordfish'
      }
    };
    function callback(error, response, body) {
      console.log(response.statusCode);
      if (error) {
        console.log(error)
            }
      if (!error &amp;&amp; response.statusCode == 200) {
        var word = JSON.parse(body);
        console.log(word);
        admin.firestore().collection('Users').doc(context.params.userID).collection('English_American').doc('Word_Response').set({
          'metadata': word.metadata,
          'results': word.results
        })
         .then(function() {
          console.log("Document written.");
        })
        .catch(function(error) {
          console.log("Error writing document: ", error);
        })
      }
    }
    request(options, callback);
  } else {
    console.log("change.after.data().word === undefined");
  }
  return 0;
  </code></pre>
  
  <p>});</p>
  
  <p>Here's the log for one function call:</p>
  
  <pre><code>12:55:12.780 PM oxford_English_US { metadata: { provider: 'Oxford University Press' }, results: [ { id: 'to', language: 'en', lexicalEntries: [Object], type: 'headword', word: 'to' } ] }
  12:55:12.480 PM oxford_English_US 200
  12:55:09.813 PM oxford_English_US Function execution took 3888 ms, finished with status: 'ok'
  12:55:09.739 PM oxford_English_US bcmrZDO0X5N6kB38MqhUJZ11OzA3
  12:55:09.739 PM oxford_English_US { word: 'to' }
  12:55:09.732 PM oxford_English_US { word: 'have' }
  12:55:05.926 PM oxford_English_US Function execution started
  </code></pre>
  
  <p>The function executed in four seconds. Three seconds after the function finishes executing, the "200" status code comes back, with the data. The data is never written to Firestore. </p>
  
  <p>It looks like the Cloud Function doesn't wait for the HTTP response. </p>
  
  <p>Here's another function log, in which the Cloud Function appears to have finished executing before sending out the HTTP request:</p>
  
  <pre><code>  1:02:29.319 PM oxford_English_US Function execution took 3954 ms, finished with status: 'ok'
    1:02:29.218 PM oxford_English_US bcmrZDO0X5N6kB38MqhUJZ11OzA3
    1:02:29.218 PM oxford_English_US { word: 'to' }
    1:02:29.213 PM oxford_English_US { word: 'the' }
    1:02:25.365 PM oxford_English_US Function execution started
  </code></pre>
  
  <p>Is there a better Node module for sending HTTP requests? <code>request</code> has this syntax:</p>
  
  <pre><code>request(options, callback);
  </code></pre>
  
  <p>I'd rather return a promise instead of a callback.</p>