I have a firebase function that is as follows:
exports.myFunction = functions.database.ref('/users/{id}/').onWrite((snapshot, context) => {
//do some processing here
//wait for 5 seconds
snapshot.after.ref.parent.once('value').then((newSnapshot) => {
//do some processing here
//check for some CONDITION
});
//do some processing here
return snapshot.after.ref.set(true); //return this if the CONDITION has been satisfied
return null; //return this if the CONDITION check fails
});
I am unable to find a suitable implementation for this logic. I am running into two problems.
Firstly, I am not sure how to delay the execution by waiting for 5 seconds. I have tried implementing the setTimeout method but I am going wrong somewhere.
Secondly, I do not know how to program so that an appropriate return value is passed depending on the CONDITION check in the inner function.
It is my first time working on firebase and I am relatively new to js so any help would be appreciated.
EDIT:
Here is an example of what I am doing.
Let us say there are two booleans internet and location_monitor. Both are true initially which has been set by the client. The location is being processed for some information. The client can set both to false at any time.
Client loses internet connection. The variable internet is changed to false. The client is notified via a local monitoring service that internet connection has been lost.
A timeout period is set for the client to come back online(5 seconds say). If the client comes online, location monitoring is reestablished and the processing continues. If the client does not come online, location monitoring is stopped. When the client comes online after a day or so, location monitoring is started as a fresh data set.