title: In which situations could I get a docSnapshot.data() with a Firestore serverTimestamp that hasn't been set yet?
body: <p>I just encountered this piece of <a href="https://firebase.google.com/docs/reference/js/firebase.firestore.SnapshotOptions" rel="nofollow noreferrer">firebase doc</a> on <code>firebase.firestore.SnapshotOptions</code>:</p>
  <blockquote>
  <p><strong>serverTimestamps</strong></p>
  <p><code>serverTimestamps: &quot;estimate&quot; | &quot;previous&quot; | &quot;none&quot;</code>
  If set, controls the return value for server timestamps <strong>that have not yet been set to their final value.</strong></p>
  <p>By specifying <code>'estimate'</code>, pending server timestamps return an estimate based on the local clock. This estimate will differ from the final value and cause these values to change once the server result becomes available.</p>
  <p>By specifying <code>'previous'</code>, pending timestamps will be ignored and return their previous value instead.</p>
  <p>If omitted or set to <code>'none'</code>, <code>null</code> will be returned by default until the server value becomes available.</p>
  </blockquote>
  <p>And that got me worried, 'cause I'm trying to convert my client generated dates to server timestamps. I thought that whenever a I read a document snapshot that was saved with a <code>serverTimestamp()</code>, I could be 100% that a date would have been set at that point. But from that documentation above, it seems that I cannot make that assumption.</p>
  <p><strong>As a general question:</strong></p>
  <p>In which situations could I find a <code>serverTimestamp</code> that hasn't been set yet? Is there a common use case where that could be an issue?</p>
  <p>Specifically I would like to know about these two cases:</p>
  <p><strong>CASE 1</strong></p>
  <ul>
  <li>Saved a document to firestore with a field <code>createdAt: firebase.firestore.FieldValue.serveTimestamp()</code></li>
  <li>I have a cloud function with a firestore <code>onWrite | onUpdate</code> trigger for that specific collection.</li>
  </ul>
  <p>When that function runs, is there a possibility that the <code>createdAt: serverTimestamp</code> has not been set yet?</p>
  <p><strong>CASE 2</strong></p>
  <ul>
  <li>Saved a document to firestore with a field <code>createdAt: firebase.firestore.FieldValue.serveTimestamp()</code></li>
  <li>On client I have a listener for that <code>doc</code> I just saved/updated.</li>
  </ul>
  <p>When that listener fires with the new <code>docSnapshot</code> data, is there a possibility that the <code>createdAt: serverTimestamp</code> has not been set yet?</p>
  <hr />
  <p><strong>UPDATE</strong></p>
  <p>Just found this article <a href="https://medium.com/firebase-developers/the-secrets-of-firestore-fieldvalue-servertimestamp-revealed-29dd7a38a82b" rel="nofollow noreferrer">The secrets of Firestore’s FieldValue.serverTimestamp()</a></p>
  <p>It seems that for the <strong>CASE 2</strong> this would only happens for local writes. Given that fact, is it safe to assume that on <strong>CASE 1</strong>, since it's not a local write, it would never happen on a cloud function that is listening to a firestore trigger?</p>
  <p><a href="https://firebase.google.com/docs/firestore/query-data/listen#events-local-changes" rel="nofollow noreferrer">Firebase docs on Local Changes</a></p>
  <blockquote>
  <p>Local writes in your app will invoke snapshot listeners immediately. This is because of an important feature called &quot;latency compensation.&quot; When you perform a write, your listeners will be notified with the new data before the data is sent to the backend.</p>
  <p>Retrieved documents have a metadata.hasPendingWrites property that indicates whether the document has local changes that haven't been written to the backend yet. You can use this property to determine the source of events received by your snapshot listener.</p>
  </blockquote>