Pier Pier - 2 months ago 7
Javascript Question

How to cancel a once() call before answer?

So what's the proper way of canceling a

.once()
call before the database has answered?

I have a use case where if the database takes more than a few seconds to answer, I don't want the answer. I'm using a promise notation, but if required, I could switch to the callback notation.

Promise notation:

firebase.database().ref('something').once('value').then(...)


Callback notation:

firebase.database().ref('something').once('value',snapshot => {...})

Answer

You can unregister listeners by calling off().

See the reference documentation for off(). It only speaks of working for on(), but it will work for once() listeners too.

I'm not sure what it would accomplish though, as the value listener will fire right away anyway.

Comments