title: selenium webdriver hang in very simple javascript code with promise.then
body: <p>I had experienced a very strange bug with nodejs &amp; selenium-webdriver recently. It looks related with nodejs event loop / promise / selenium-webdriver.</p>
  
  <p>In short: <strong>webdriver.get call will hang when use with some <code>promise.then</code>.</strong></p>
  
  <h1>First, let's see a piece of javascript code which works well:</h1>
  
  <pre><code>const WebDriver     = require('selenium-webdriver')
  
  var driver = new WebDriver.Builder()
  .setAlertBehavior('ignore')
  .forBrowser('chrome')
  .build()
  
  function ok() {
    console.log('ok: driver.get will open a browser window\n' + '\n' + ok + '\n')
    driver.get('about:blank')
  }
  
  ok()
  </code></pre>
  
  <p>This code will open a browser window, which works as expected.</p>
  
  <p>But all of the following code will show strange behaviour.</p>
  
  <h1>Bug - driver.get will hang</h1>
  
  <p>after add a dummy <code>.then(() =&gt; 'test')</code></p>
  
  <pre><code>function bug() {
    console.log('bug: `driver.get` will hang\n' + '\n' + bug + '\n')
    Promise.resolve()
    .then(() =&gt; 'test')
    .then(() =&gt; driver.get('about:blank'))
  }
  </code></pre>
  
  <h1>Fix1 - delete the 1st <code>then</code> call</h1>
  
  <pre><code>function fire1() {
    console.log('fix1: delete the `then` call\n' + '\n' + fire1 + '\n')
    Promise.resolve()
    .then(() =&gt; driver.get('about:blank'))
  }
  </code></pre>
  
  <h1>Fix2 - or add a <code>catch</code> call</h1>
  
  <pre><code>function fire2() {
    console.log('fix2: add a `catch` call\n' + '\n' + fire2 + '\n')
    Promise.resolve()
    .then(() =&gt; 'test')
    .catch(e =&gt; console.error(e))
    .then(() =&gt; driver.get('about:blank'))
  }
  </code></pre>
  
  <h1>Fix3 - or resolve the promise with a <code>setTimeout</code> instead of resolve directly</h1>
  
  <pre><code>function fire3() {
    console.log('fix3: resolve promise with a `setTimeout` call\n' + '\n' + fire3 + '\n')
    new Promise((resolve, reject) =&gt; {
      setTimeout(resolve, 0)
    })
    .then(() =&gt; 'test')
    .then(() =&gt; driver.get('about:blank'))
  }
  </code></pre>
  
  <p>All of the above code is tested under nodejs v6.0.0.</p>
  
  <p>Has there anybody met this kind of problem before? What's the reason behind this?</p>
  
  <p>I thought it maybe related with the nodejs event loop, or native promise implementation... but I'm dont know how to deep any more.</p>
  
  <p>Code in Gist: <a href="https://gist.github.com/zixia/77896cbd446c7282f760c60a025fee17" rel="nofollow">https://gist.github.com/zixia/77896cbd446c7282f760c60a025fee17</a></p>
  
  <p>Github issue: <a href="https://github.com/SeleniumHQ/selenium/issues/2233" rel="nofollow">https://github.com/SeleniumHQ/selenium/issues/2233</a></p>