If I run the following line of code when my device is offline, The card
will be added to local cache. But the application never goes to the next line.
const data = await firebase.firestore().collection('cards').add(card);
console.log(data) // this line never get executed!
Write methods (such as add()
) return a promise that fulfills once the write operation has been completed (or rejected) on the server. Since you're not connected to the server, that never happens and thus your await
will stay blocked.
Note that you don't need to await
for the local write operation, as the write to the local cache happens synchronously. So as soon as the add(...)
call returns (and doesn't raise an exception), you can be sure that the write was completed to the local cache. Hence, you should only use await
if you need to know that the write was also handled by the server, in which case the behavior you see is exactly what you'll want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With