Skip to content

transaction(...).read(...).write(...) returns Promise even tho the types say otherwise #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dorklein opened this issue Apr 9, 2025 · 0 comments

Comments

@dorklein
Copy link

dorklein commented Apr 9, 2025

Firebase Firestore runTransaction returns a Promise as you can see in the official docs

const cityRef = db.collection('cities').doc('SF');
try {
  //     here 👇
  const res = await db.runTransaction(async t => {
    const doc = await t.get(cityRef);
    const newPopulation = doc.data().population + 1;
    if (newPopulation <= 1000000) {
      await t.update(cityRef, { population: newPopulation });
      return `Population increased to ${newPopulation}`;
    } else {
      throw 'Sorry! Population is too big.';
    }
  });
  console.log('Transaction success', res);
} catch (e) {
  console.log('Transaction failure:', e);
}

But typesaurus types doesn't indicate that the result is a Promise and some transactions might not execute if you exit after the transaction (E.G. in firebase cloud functions, the function might die before the transaction has finished because we didn't await it)

The return type of write in typesaurus is WriteDocsToDocs<WriteResult, Props>

const result = transaction(...).read(...).write((...) => {
  // .... Transaction write ....
  return 'DONE'
}) 

// Typescript -> result: string
// Actual -> 
console.log("result", result)
// result Promise {
//  <pending>,
//...
//},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant