2
votes

When I want to do multiple writings, deletes or updates to Cloud Firestore from within Cloud Functions, I normally do this with promises:

var proms = []
proms.push(sometask)
return Promises.all(proms)

However I came across batches: https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes

I think this would look like this:

var batch = db.batch();
batch.update(sometask)
return batch.commit();

What are the difference between those two?

1
Note that batched writes are a concept in Cloud Firestore, while you classified the question as being about Cloud Functions. I retagged and edited, but please double-check. If the difference between the two is not clear, please ask. - Frank van Puffelen

1 Answers

5
votes

When you do Promise.all on a number of operations, those operations are still sent to the server one by one. Each operation can fail individually, while others succeed.

When you use a batched write (or transaction), your operations are sent to the server in one command. This means that they will either all fail, or all succeed.