title: Firebase Realtime Database - Wait for multiple queries to finish
body: <p>I am working with the Firebase Realtime Database and would like to collect all the "notes" I have access to - ie. where I am listed as "author" or invited "member". </p>
  
  <p>My database structure illustrated below.
  <a href="https://i.stack.imgur.com/RGwpA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RGwpA.png" alt="Database structure"></a></p>
  
  <p>I am currently using the below code to fetch all "notes" I have access to.</p>
  
  <pre><code>return new Promise( (resolve, reject) =&gt; {
    try{
      let notes = []
  
      //Get notes where I am author : uid = my user uid
      database.ref('notes')
      .orderByChild("access/author")
      .equalTo(`${uid}`)
      .on('value', (snapshot) =&gt; {
        snapshot.forEach( (childSnapshot) =&gt; {
          const note = {
            id: childSnapshot.key,
            ...childSnapshot.val()
          }
          notes.push(note)
          })
        })
  
      //Get notes where I am member
      database.ref('notes')
      .orderByChild(`access/members/${uid}`)
      .equalTo(true)
      .on('value', (snapshot) =&gt; {
        snapshot.forEach( (data) =&gt; {
          const note2 = {
            id: data.key,
            ...data.val()
          }
          notes.push(note2)
        })
      })
      //Return all notes
      resolve(notes)
    } catch(e) {}
  })
  </code></pre>
  
  <p>The code does not work. The resulting array does not include notes where I am listed as "member". I suspect the reason being that the database calls are synchronous - they do not wait to be completed before returning the array of all notes. </p>
  
  <p>How can I make sure all notes are fetched before resolving the array? </p>
  
  <p>Kind regards /K</p>