title: How to prevent Timing Problems When Using Closures?
body: <p>I'm trying to find the sum of numbers, returned from different escaping closures. The sum to be returned in main thread.</p>
  
  <pre><code>import Foundation
  
  var randomTime: Int {
    return Int.random(in: 0...1000)
  }
  
  func first(completion: @escaping (Int) -&gt; Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(randomTime)) {
      completion(1)
    }
  }
  
  func second(completion: @escaping (Int) -&gt; Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(randomTime)) {
      completion(2)
    }
  }
  
  func third(completion: @escaping (Int) -&gt; Void) {
    DispatchQueue(label: "anotherThread").async {
      completion(3)
    }
  }
  
  func fourth(completion: @escaping (Int) -&gt; Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(randomTime)) {
      completion(4)
  }
  }
  </code></pre>