title: Firestore, onSnapshot() or async/await issue or both
body: <p>I'm trying to get an array of changes using Firebase Firestore's <a href="https://firebase.google.com/docs/firestore/query-data/listen" rel="nofollow noreferrer">onShapshot()</a>.</p>
  <p>I'm having trouble retrieving data through <code>onSnapshot()</code>; I may be in trouble with <code>async/await</code> as well, not quite sure...</p>
  <p>Can you see where there are problems?</p>
  <p>Output should be (but it is currently):</p>
  <pre><code>1. New friends: ... // via onSnapshot(). Should not be empty, but it is (However, it does get populated afterwards).
  2. All friends: ... // Should not be empty, but it is.
  3. Fred's friends: ... // Should not be empty, but it is.
  </code></pre>
  <p>Code:</p>
  <pre><code>const getAllFriends = async () =&gt; {
      // Gets all friends by connecting to Firestore's onSnapshot stream.
  
      const getNewFriends = async () =&gt; {
          // Sets up a onSnapshot() stream, and returns a newFriends array with their names.
          // Problem: It initially return an empty array, when it shouldn't be empty.
  
          let newFriends = [];
          await db.collection(&quot;user&quot;).doc(&quot;john&quot;).collection(&quot;friends&quot;).onSnapshot(snapshot =&gt; {
              snapshot.docChanges().forEach(change =&gt; {
                  newFriends.push({ friend: &quot;Emily&quot; });
              });
          });
  
          console.log(&quot;1. New friends: &quot;, newFriends, newFriends.length); // Length should not be 0.
          return newFriends;
      }
  
      // John starts with no friends:
      let friends = []; 
  
      // John should now have found some friends:
      let friendChanges = await getNewFriends(); 
      friends = friends.concat(friendChanges);
  
      console.log(&quot;2. All friends:&quot;, friends); // Should contain a few Emilys.
      return friends;
  };
  
  let johnFriends = await getAllFriends();
  console.log(&quot;3. John's friends:&quot;, friends); // Should contain a few Emilys.
  </code></pre>