async function transaction()

in firestore/main/index.js [368:396]


async function transaction(db) {
  // [START firestore_transaction_document_update]
  // Initialize document
  const cityRef = db.collection('cities').doc('SF');
  await cityRef.set({
    name: 'San Francisco',
    state: 'CA',
    country: 'USA',
    capital: false,
    population: 860000
  });

  try {
    await db.runTransaction(async (t) => {
      const doc = await t.get(cityRef);
  
      // Add one person to the city population.
      // Note: this could be done without a transaction
      //       by updating the population using FieldValue.increment()
      const newPopulation = doc.data().population + 1;
      t.update(cityRef, {population: newPopulation});
    });

    console.log('Transaction success!');
  } catch (e) {
    console.log('Transaction failure:', e);
  }
  // [END firestore_transaction_document_update]
}