Getting the value of array out of scope of a function [duplicate]

I want to know if there is any way to get the value of ref inside an async function.
the code below show the example for my problem:

var results =ref([])
    projectFirestore.collection('patients').where('ssn', '==', '211').onSnapshot((snap) => {
            snap.docs.forEach((doc) => {
              results.value.push(doc.data());
            })  
            
      })
    console.log(results.value.length);  

the output of the console is 0, but if I move the console function inside the function it will print ’21’ (the length of array that retrieved from DB)