Should a Firebase DB Triggered Function return something when its execution ends?

Viewed 66

Does a Firebase Database Trigger Function need to return something? I mean, as it is not a callable function... the nobody will receive the returned result so, is it neccessary?

I am asking this because I am afraid of Cold Starts, I have read that an unhandled exception can cause it, so maybe (possibly not), if I don't return something at the end of the function the execution continues until some timeout exception happens or something like that, causing a Cold Start.

I will really appreciate your answer. Thank you.

2 Answers

I'm going to disagree with Frank and suggest that the return value isn't important at all for background functions. The only requirement you have is to return a promise that resolves after all the work is complete. This is covered in the documentation. What value that promise resolves to is totally unimportant. The final outcome of the function does not change based on the resolved value. If your compiler/linter suggests that you should return something, you can simply return null.

Yes, background Cloud Functions like those triggered by Firebase Realtime Database or Cloud Firestore must always return something. The reason for this is that the return value is used to determine when the function is done.

If there's no sensible return value, you can return a true or null, or a Promise that resolve to true or null.

Related