title: How to refresh the page until element is disappeared in selenium with javascript
body: <p>In selenium JavaScript, I have to refresh the page until element is disappeared. Since it uses promises,
  if I use for loop it will create many promise even if the condition is satisfied, later it will be resolved. Also I have to provide driver.sleep(5000) mandatory due to some reason.</p>
  <pre><code>$browser.get(&quot;https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html&quot;).
  then( function()
  {
  var p=Promise.resolve();
  for (let index = 0; index &lt; 5; index++) {    
      p = p.then(() =&gt; 
      isElementDisplayedAfterrefresh($driver.By.xpath(&quot;//span[text()='PouchDB']&quot;), &quot;input&quot;))
      .then(a=&gt;$browser.sleep(3000))
      .then(()=&gt;$browser.navigate().refresh())
  }
  return p;
  }
  ).then(
      ()=&gt;console.log('done'))
  </code></pre>
  <p>code should exit if element is not displayed, it should refresh if element is still displayed.
  How to loop through promise sequentially when we don't know results/number of iterations
  the return type is Promise, How to break loop? thanks</p>
  <h2>Edit</h2>
  <p><a href="https://gist.github.com/saiparth/045c32267e55b836f139bdac6597e57b" rel="nofollow noreferrer">https://gist.github.com/saiparth/045c32267e55b836f139bdac6597e57b</a>
  Issue is it schedules commands for loop, so all 5 iteration will execute no matter for previous index worked not. Also I should not use async/await</p>