title: Swift - multiple Chain http request with loop
body: <p>Since 2 days it feels that I'm searching the whole web to solve my problem with multiple http requests. So my workflow looks like this:</p>
  
  <ol>
  <li><p>Upload a image to a server</p>
  
  <ul>
  <li>Response = XML Format with a Task ID</li>
  </ul></li>
  <li><p>GET request to the server with the Task ID to check the status of this task.</p>
  
  <ul>
  <li>Response = XML Format where the status could be "Completed", "In Progress", "Queued"</li>
  <li>If Status != "Completed" - retry step 2</li>
  <li>If Status == "Completed" - go to step 3</li>
  </ul></li>
  <li><p>Download the result from the resultUrl</p></li>
  </ol>
  
  <p>My last try was to use <code>PromiseKit</code> to chain the requests in a clean way like described in this post: <a href="https://stackoverflow.com/questions/28634995/chain-multiple-alamofire-requests">Chain multiple Alamofire requests</a>. But I don't know how to loop the second step every 2-5 seconds if the status isn't completed yet.</p>
  
  <p>Is there a recommended solution for this workflow? This was my test with <code>PromiseKit</code>, where i successfully chained the requests without a loop:</p>
  
  <pre><code>let request = Client.imageUploadRequest(image: imageView.image!)
  let httpOperation = HTTPOperation(withRequest: request)
  
  httpOperation.sendRequest().then() { result -&gt; Promise&lt;String&gt; in
      let xml = SWXMLHash.parse(result)
      let id = self.getXMLAttribute(from: xml, with: "id")!
      let taskStatusrequest =  Client.getTaskStatusRequest(withTaskID: id)
      let httpOperation = HTTPOperation(withRequest: taskStatusrequest)
  
      return httpOperation.sendRequest()
  }
  // Loop this result if status != "Completed"
  .then { result -&gt; Promise&lt;Data&gt; in
      let xml = SWXMLHash.parse(result)
      let downloadUrl = self.getXMLAttribute(from: xml, with: "resultUrl")!
      let downloadRequest = Client.getDownloadRequest(withUrl: downloadUrl)
      let httpOperation = HTTPOperation(withRequest: downloadRequest)
  
      // if status != "Completed" don't return, retry this step
      return httpOperation.downloadData()
  }
  .then { _ -&gt; Void in
      // Refresh View with data
  }
  </code></pre>