title: Swift: Async method into while loop
body: <p>I want to use a async function inside a while loop but the function don't get enough time to finish and the while loop starts again and never ends.
  I should implement this problem with increment variable , but what is the solution? thanks a lot.<br>
  output loops between "Into repeat" - "Into function"</p>
  
  <pre><code>var condition = true
  var userId = Int.random(1...1000)
  repeat {
       print("Into repeat")
       checkId(userId, completionHandler: { (success:Bool) -&gt; () in
       if success {
             condition = false
       } else {
             userId = Int.random(1...1000)
         }
  }) } while condition
  
  
  func checkId(userId:Int,completionHandler: (success:Bool) -&gt; ()) -&gt; () {
          print("Into function")
          let query = PFUser.query()
          query!.whereKey("userId", equalTo: userId)
          query!.findObjectsInBackgroundWithBlock({ (object:[PFObject]?, error:NSError?) -&gt; Void in
              if object!.isEmpty {
                  completionHandler(success:false)
              } else {
                  completionHandler(success:true)
              }
          })
      }
  </code></pre>