title: Dealing with multiple completion handlers
body: <p>I'm trying to coordinate several completion handlers for each element in an array. </p>
  
  <p>The code is essentially this:</p>
  
  <pre><code>var results = [String:Int]()
  
  func requestData(for identifiers: [String])
  {
      identifiers.forEach
      {   identifier in
  
          service.request(identifier, completion: { (result) in
              result[identifier] = result
          })
      }
  
      // Execute after all the completion handlers finish
      print(result)
  }
  </code></pre>
  
  <p>So each element in the Array is sent through a service with a completion handler, and all the results are stored in an array. Once all of these handlers complete, I wish to execute some code.</p>
  
  <p>I attempted to do this with <code>DispatchQueue</code></p>
  
  <pre><code>var results = [String:Int]()
  
  func requestData(for identifiers: [String])
  {
      let queue = DispatchQueue.init(label: "queue")
  
      identifiers.forEach
      {   identifier in
  
          service.request(identifier, completion: { (result) in
              queue.sync
              {
                  result[identifier] = result
              }
          })
      }
  
      // Execute after all the completion handlers finish
      queue.sync
      {
          print(result)
      }
  }
  </code></pre>
  
  <p>but the print call is still being executed first, with an empty <code>Dictionary</code></p>