1

I recently asked a question on async calls in firebase functions. The response to the question was that it was not possible to guarantee an async call in a function would complete all it's tasks once the function is completed with either return; or res.end();, whichever is relevant (See: What happens to async calls in completed firebase functions?).

However what I notice - especially when my functions or the database are not 'hot' - is that a function would take unnecessarily long to respond to the user. Consider the following code where a function call would increment a value in the database. Note that the user does not need to know when the database is finished updating or the value after the update:

import * as firebase from 'firebase';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

exports.action = functions.https.onRequest(async (req, res) => {
    await admin.firestore().collection('actions').doc('action1')
        .update({ completedActions: admin.firestore.FieldValue.increment(1) });
    res.end({ message: 'One more action registered' });
});

Now the dilemma: If I would run this code with the await I can guarantee that the async call will be finished executing within the lifetime of the function. If I remove the await statement the user will see the response much faster but there is a chance that the function and it's 'child' async call will be terminated preemptively as the function is considered finalized.

Is there any way to respond to the user asap while still guaranteeing that all spawned async calls will finalize (if they would otherwise have finalized within the timeoutSeconds defined for the function)?

Raven
  • 1,453
  • 3
  • 18
  • 29

0 Answers0