title: Latency compensation and Firestore transactions
body: <p>I have an onSnapshot keeping track of the documents in a collection:</p>
  <p><code>db.collection('/.../').onSnapshot(querySnapshot=&gt; mylocalvariable = querySnapshot.docs)</code></p>
  <p>Now, I want to select the first (in some order) element of this collection of documents that my user has not yet handled. When a user is done handling a document, I use a transaction to update the document according to the user's needs (transaction is better for me than .update() because I might have multiple users changing different parts of the document).</p>
  <p>The problem is that unlike a .update (which would update mylocalvariable immediately), it seems like the transaction finishes without updating mylocalvariable. So, when I go to grab the &quot;next&quot; document, it just grabs the same document, because the function runs before the variable gets updated.</p>
  <p>Code sample:</p>
  <pre><code>db.collection('/mycollection').onSnapshot(querySnapshot=&gt; mylocalvariable = querySnapshot.docs)
  
  function selectnextrecord(){
    nextrecord = mylocalvariable.find(x=&gt;!x.data().done)
    console.log(nextrecord)
    //expected: Get something different than the current record
    //observed: This is being run with old data, so it returns the same record that I currently have with the old data.
  }
  let nextrecord;
  selectnextrecord();
  
  function submitchanges(){
     let sfDocRef = db.collection('/mycollection').doc(nextrecord.id);
     return db.runTransaction(function(transaction) {
        return transaction.get(sfDocRef).then(function(sfDoc) {
           if (!sfDoc.exists) {
              throw &quot;Document does not exist!&quot;;
           }
           transaction.update(sfDocRef, {done:true});
        });
     }).then(function() {
              selectnextrecord();
     }).catch(function(error) {
        console.log(&quot;Transaction failed: &quot;, error);
     });
  }```
  </code></pre>