title: Wait for a function with an async operation to finish
body: <p>How can I wait for a function that itself has an async operation to finish?</p>
  
  <p><em>Let me give you an example:</em></p>
  
  <p><code>Function_A</code> fetches a photo from gallery. Fetching operation will be executed asynchronously using fetching closure. </p>
  
  <p>I call <code>Function_A</code> in <code>Function_B</code> and I want to wait the photo fetching operation to finish, and after that continue my code.</p>
  
  <p><em>What I've tried:</em></p>
  
  <p>I've used dispatch group but it has not worked. I've also tried to call <code>Function_A</code> in a sync block, and this was unsuccessful too.</p>
  
  <p>How can I wait for this operation to finish? Can be this problem because of running fetching operation into another queue?</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>This is what I did:</p>
  
  <pre><code>class imageFetch: NSObject{
      var theImage: PHAsset? = nil
      func Function_A(){
          let fetchOption = PHFetchOptions()         
          PHPhotoLibrary.requestAuthorization { (authStatus) in
              // It seems code will be executed asynchronously from here
              if authStatus == .authorized{
                  let imageAsset = PHAsset.fetchAssets(with: .image, options: fetchOption)
              // Select image and save it in theImage
              // I know I can send a closure and run it here
              }
          }
      }
  }
  </code></pre>
  
  <p>And in another class I call that  function:</p>
  
  <pre><code>class edit: NSObject{
      funct Function_B(){
          var imageFetchIns = imageFetch()
          // I want below code to execute asynchronously
          imageFetchIns.Function_A()
  
          // Now I want to wait last operation to finish
      }
  }
  </code></pre>