title: What Does 'Then' Really Mean in CasperJS
body: <p>I'm using CasperJS to automate a series of clicks, completed forms, parsing data, etc through a website.</p>
  
  <p>Casper seems to be organized into a list of preset steps in the form of <code>then</code> statements (see their example here: <a href="http://casperjs.org/quickstart.html" rel="noreferrer">http://casperjs.org/quickstart.html</a>) but it's unclear what triggers the next statement to actually run.</p>
  
  <p>For example, does <code>then</code> wait for all pending requests to complete? Does <code>injectJS</code> count as a pending request? What happens if I have a <code>then</code> statement nested - chained to the end of an <code>open</code> statement?</p>
  
  <pre><code>casper.thenOpen('http://example.com/list', function(){
      casper.page.injectJs('/libs/jquery.js');
      casper.evaluate(function(){
          var id = jQuery("span:contains('"+itemName+"')").closest("tr").find("input:first").val();
          casper.open("http://example.com/show/"+id); //what if 'then' was added here?
      });
  });
  
  casper.then(function(){
      //parse the 'show' page
  });
  </code></pre>
  
  <p>I'm looking for a technical explanation of how the flow works in CasperJS.  My specific problem is that my last <code>then</code> statement (above) runs before my <code>casper.open</code> statement &amp; I don't know why.</p>