Search code examples
firebasegoogle-cloud-firestorees6-promise

Difference between batch and promises


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?


Solution

  • 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.