Can a cloud function safely execute async work after it has returned its promise? Consider the following pattern:
exports.deleteUser = functions.auth.user().onDelete(async (user) => {
const uid = user.uid;
asyncTask1(uid);
asyncTask2(uid); // don't wait for the last task to finish
asyncTask3(uid); // just attempt them all and handle
asyncTask4(uid); // the failures individually later
return Promise.resolve(uid);
});
async function asyncTask1(uid) {
try {
await someAsyncWork();
return Promise.resolve(true);
} catch (error) {
throw new functions.https.HttpsError("unknown", "Task 1 failed.", error);
}
}
Should this pattern be avoided in Cloud Functions? My concern is that the instance that was initialized to execute this call could be deallocated before the tasks have a chance to finish since the function that initialized the instance returned a promise almost immediately after being called. Or will the server keep this instance alive to let the async work finish even though the main function has already terminated?