I am relatively new to the async world of Javascript. My function makes some calls to the Firebase Admin SDK and also some fetch requests to a third party API. The function works, but every once in a while I get a socket hang up error. I essentially want to wait until all the await functions complete before sending res.end(), but it looks like I am doing something wrong in terms of my use of async/await.
My function finishes (looks like it hits res.end()) but it keeps continuing on:
And then strangely enough, an error from the same execution ID shows up in the following execution ID:
Here is how I am structuring my code:
exports.myFunction = async (req, res) => {
// parse the body...
// if it's a closed order
if (json.hasOwnProperty('closed_at')) {
// get order object from orders directory
await ordersRef.once("value", async function(snapshot) {
var orderResponseObj = snapshot.val();
// do some stuff with orderResponseObj
// ** res.end() CALLED BEFORE THIS HAPPENS **
await deleteRef.once("value", async function(snapshot) {
var deleteResponseObj = snapshot.val();
// do some stuff with the delete object
});
// get object from shop directory
await shopInfoRef.once("value", async function(snapshot) {
var firebaseResponseObj = snapshot.val();
await updateInventory.updateInventory(); // do some fetch request to 3rd party API
await deleteProduct.deleteProduct(); // do another fetch call to 3rd party API
});
});
}
// terminate the cloud function
res.end();
}

