Reputation: 1967
Let's say I have a cloud function like this
export const setData = functions.https.onCall(async (data, context) => {
await admin.firestore().doc("myPath").set({data: "myData"})
return {success: true}
})
If I don't care about the firestore set call is successful or not, can I remove the await? So the function returns earlier and reduces CPU usage time.
export const setData = functions.https.onCall(async (data, context) => {
admin.firestore().doc("myPath").set({data: "myData"})
return {success: true}
})
Upvotes: 2
Views: 2214
Reputation: 121
This is more like JS question than Firebase itself.
If you really don't care, no problem but you'll never know if anything returned or not, even if the function could be called normally as the return might just finish everything before the Firebase call is complete.
"returns earlier and reduces CPU usage time."
Using less CPU might be just a signal that your function is not working properly.
Also, Firebase is really fast and scalable, I would always recommend then/catch
's to keep execution and catch errors, even just for logging purposes. Something like:
admin.firestore().doc("myPath").set({data: "myData"}).then(response => {
return whateverYouWant(response)
}).catch(err => {
return handleWhateverError(err)
})
Also, there's no need to keep async
if you have no await
in the function anymore.
Upvotes: 0
Reputation: 317362
You will have problems if you remove the await. Without it, the function will return immediately with the return value without waiting for the set() to complete. The function will terminate along with any asynchronous work that's not finished.
With a callable function, the function must return with a promise that resolves only after all of the async work is complete. The await keyword makes that happen in your case.
Upvotes: 2