title: JavascriptExecutor SyntaxError: Unexpected identifier. Why?
body: <p>Java</p>
  
  <pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;
  Boolean ready = (Boolean)js.executeScript("the below JavaScript");
  </code></pre>
  
  <p>JavaScript</p>
  
  <pre><code>var ready = false;
  window.onload = function() {
      ready = true;
  }
  window.sleep = function() {
      return new Promise(resolve =&gt; setTimeout(resolve, 2000));
  }
  for(var i = 0; i &lt; 30; i++) {
      if(ready) {
             return true;
      }
      await sleep();
   }
   return false;
  </code></pre>
  
  <p>UPDATE: <em>Sorry about the previous syntax error "funtion" in my post. That was a typo not in my actual code. All syntax errors should be gone, but I still get "SyntaxError: Unexpected identifier".</em></p>
  
  <p>What this code is trying to do is wait for a maximum amount of time for the page to be loaded. I typically return document.readyState to check for that condition but in unique circumstances Chrome will suddenly stop loading the page and document.readyState hangs for 5+ minutes. This is killing my code so I am attempting to develop single-threaded code to kind of mimic a typically multi-threaded process.</p>
  
  <p>Since JavaScript is single threaded (such a disappointing feature for a cool language like JavaScript), we have to be creative.</p>
  
  <p>This code works in the browser console if you replace <code>return true;</code> with <code>console.log('true');</code> and <code>return false;</code> with <code>console.log('false');</code> so I don't see what the problem is.</p>