title: My return value is nil and I don't know why(Swift)
body: <pre><code>func getUsername() -&gt; String {
      var username: String!
  
       Firestore.firestore().collection(&quot;UserData&quot;).document(&quot;\(userID!)&quot;).getDocument { (document, err) in if let document = document, document.exists {
           let data = document.data()
           username = data![&quot;username&quot;] as? String
      
          }
      }
      return username
  }
  
  func getUsername() -&gt; String { 
  var username: String! Firestore.firestore().collection(&quot;UserData&quot;).document(&quot; 
   (userID!)&quot;).getDocument { (document, err) in if let document = document, 
  document.exists { let data = document.data() username = data![&quot;username&quot;] as String 
  return username 
  } 
  } 
  }
  </code></pre>
  <p>It is saying that username is <strong>nil</strong>. It appears its returning before fetching the data, how do I fix this?</p>