title: phantomJS: Unable to access network
body: <p>I have a phantomJS script</p>
  
  <pre class="lang-js prettyprint-override"><code>var page = require('webpage').create();
  var system = require('system');
  
  page.settings.userAgent = 'SpecialAgent';
  
  var i = 1;
  var url = 'http://www.google.cz/?test=' + i;
  console.log( "url='" + url + "'" );
  page.open(url, function (status) {
      if (status !== 'success') {
          console.log( 'Unable to access network' );
      } else {
          console.log( 'Opened ok' );
          phantom.exit();
      }
  });
  </code></pre>
  
  <p>which works fine, but when I wrap the <code>page.open</code> in for loop as</p>
  
  <pre class="lang-js prettyprint-override"><code>var page = require('webpage').create();
  var system = require('system');
  
  page.settings.userAgent = 'SpecialAgent';
  
  //var i = 1;
  for ( var i = 1; i &lt;= 2; ++i ) {
      var url = 'http://www.google.cz/?test=' + i;
      console.log( "url='" + url + "'" );
      page.open(url, function (status) {
          if (status !== 'success') {
              console.log( 'Unable to access network' );
          } else {
              console.log( 'Opened ok' );
              phantom.exit();
          }
      });
  }
  </code></pre>
  
  <p>I'm getting</p>
  
  <blockquote>
    <p>Unable to access network</p>
  </blockquote>
  
  <p>I think it is because it is executed in parallel. Am I correct? (<code>phantom.exit()</code> is in wrong place probably) How can I wait for processing <code>page.open</code> and then execute next iteration?</p>