title: Swift execute asynchronous tasks in order
body: <p>I have a few asynchronous, network tasks that I need to perform on my app. Let's say I have 3 resources that I need to fetch from a server, call them <code>A</code>, <code>B</code>, and <code>C</code>. Let's say I have to finish fetching resource <code>A</code> first before fetching either <code>B</code> or <code>C</code>. Sometimes, I'd want to fetch <code>B</code> first, other times <code>C</code> first.</p>
  
  <p>Right now, I just have a long-chained closure like so:</p>
  
  <pre><code>func fetchA() {
    AFNetworking.get(completionHandler: {
      self.fetchB()
      self.fetchC()
    })
  }
  </code></pre>
  
  <p>This works for now, but the obvious limitation is I've hard-coded the order of execution into the completion handler of <code>fetchA</code>. Now, say I want to only <code>fetchC</code> after <code>fetchB</code> has finished in that completion handler, I'd have to go change my implementation for <code>fetchB</code>... </p>
  
  <p>Essentially, I'd like to know if there's some magic way to do something like:</p>
  
  <pre><code>let orderedAsync = [fetchA, fetchB, fetchC]
  orderedAsync.executeInOrder()
  </code></pre>
  
  <p>where <code>fetchA</code>, <code>fetchB</code>, and <code>fetchC</code> are all async functions, but <code>fetchB</code> won't execute until <code>fetchA</code> has finished and so on. Thanks!</p>