The Firebase documentation claims:
A function calling a transaction (transaction function) might run more than once if a concurrent edit affects a document that the transaction reads.
It is unclear exactly which function will run twice when there is a concurrent edit. In the example below, outerFunc calls the runTranscation method, so a literal interpretation of the documentation would imply that that outerFunc may be called twice.
However, this seems like a strange implementation, and I suspect that the docs mean to say that innerFunc may be called more than once when there is a concurrent edit.
import { runTransaction } from "firebase/firestore";
function outerFunc () {
console.log('outerFunction');
try {
await runTransaction(db, async function innerFunc(transaction) {
const sfDoc = await transaction.get(sfDocRef);
if (!sfDoc.exists()) {
throw "Document does not exist!";
}
const newPopulation = sfDoc.data().population + 1;
transaction.update(sfDocRef, { population: newPopulation });
});
console.log("Transaction successfully committed!");
} catch (e) {
console.log("Transaction failed: ", e);
}
}
Could outerFunc be called twice when there is a concurrent edit as the doc imply?
There's no way the Firestore SDK can invoke your outerFunc (let alone multiple times), but your innerFunc callback may be called multiple times by the Firestore SDK indeed.
If you find the documentation confusing, please file feedback on the page itself. There should be a link for that at the bottom right.