title: Use result of async library once it’s done
body: <p>I’m using the <a href="https://github.com/amosavian/FileProvider" rel="nofollow noreferrer">FileProvider library</a> to look for files on my FTP server and Swift 5’s basic functionality to look for files in the “Documents“ folder of the device:</p>
  
  <pre><code>func lookForFiles() { //runs on a background thread
      var ftpExists = false
      var localExists = false
  
      let ftpFile:FileObject = lookForFTPFile()
      let localFile:FileObject = lookForLocalFile()
  
      //Compare,... files
  }
  
  func lookForFTPFile() -&gt; FileObject? {
      var found:FileObject?
  
      ftpProvider?.contentsOfDirectory(path: mypath, completionHandler: { (contents, error) in
          //Look for ftp file
      }) //This is run in an async task according to documentation
  
      return found
  }
  </code></pre>
  
  <p>This of course always returns "nil" because of the task in "contentsOfDirectory" (I also can't return the file from within).</p>
  
  <p><strong>Question:</strong> How do I wait for <code>lookForFTPFile</code> to finish before returning the result (which might be nil because it simply didn't find anything) - without just setting up a timer?
  I'd prefer to not mess with how the library sets up its asynchronous work.</p>
  
  <p>Something like </p>
  
  <pre><code>var waitingbool = false
  var found:FileObject?
  
  func lookForFiles() { //runs on a background thread
      //Rest of code
      lookForFTPFile()
      while !waitingbool {}
      //Use "found"
  }
  
  func lookForFTPFile() {
      ftpProvider?.contentsOfDirectory(path: mypath, completionHandler: { (contents, error) in
          //Look for ftp file and save result in "found"
          self.waitingbool = true
      }) 
  }
  </code></pre>
  
  <p>looks like might work but at the same time it seems to be breaking a lot of unwritten rules.</p>