Skip to content
Advertisement

Realtime database cloud function sends result back before finishing

I’m trying to perform some realtime database tasks, which after completion, should send back a result back to the client so the client knows when the tasks have finished.

JavaScript

I’ve tried to create a promise due to the fact some realtime database tasks do not return a promise – wasn’t sure which one. Based on the logs, the function completed with a status code of 200 and then a couple seconds after, the realtime database gets updated with the values. The database should be updated, the result sent back to the client and then the function should finish. It’s currently sending back NULL to the client – presuming the function is sending it back as soon as it runs.

How does one perform realtime database tasks one after another efficiently?

Advertisement

Answer

The following modifications should do the trick:

JavaScript

So, you need to chain the Promises and return the result to the client as shown above: as explained in the doc “the data returned by the promise is sent back to the client” (which is the case if we return the Promises chain) and the data shall be an object/value that can be JSON encoded (which is the case with the { gameChosen: gameChosen, gameAdmin: adminName, ...} object).

For the getGameQuestions() function, since the once() method returns a Promise, you don’t need to encapsulate it into a Promise. Again, just return the promises chain composed by once().then(...).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement