title: Sequential execution flow with asynchronous operations and UI
body: <p>Often, in GUI, I'm faced with a program flow which is essentially sequential, but involves asynchronous operations (such as <code>NSURLConnection</code> downloading stuff) and UI actions (wait for the user to choose an option in an <code>UIActionSheet</code>). </p>
  
  <p>Just to illustrate, for example,</p>
  
  <ol>
  <li>Show a <code>UIActionSheet</code>, prompting the user to choose a color.</li>
  <li>If color is blue, download a file from a server.
  <ol>
  <li>If download fails, inform the user (<code>UIAlertView</code>, wait for tap on OK)</li>
  <li>If user chooses YES in the <code>UIAlertView</code>, retry downloading.</li>
  </ol></li>
  <li>if color is black, do something else.</li>
  <li>and so on and so on.</li>
  </ol>
  
  <p>The flow is sequential - we don't do 2 before 1. But because of async operations and UI elements, this very quickly becomes spaghetti code if we use delegates (or blocks).</p>
  
  <p>Is there a general approach to writing such code? </p>