title: Nightmare conditional wait()
body: <p>I'm trying to crawl a webpage using Nightmare, but want to wait for <code>#someelem</code> to be present, only if it actually exists. Otherwise, I want Nightmare to move on. How can this be done using <code>.wait()</code>?</p>
  
  <p>I can't use <code>.wait(ms)</code>. Using <code>.wait(selector)</code> means Nightmare will keep waiting until the element is present, but if the page will never have this element, Nightmare will keep waiting forever.</p>
  
  <p>The last option is to use <code>.wait(fn)</code>. And I've tried something like this</p>
  
  <pre><code>.wait(function(cheerio) {
              var $ = cheerio.load(document.body.outerHTML);
              var attempt = 0;
  
              function doEval() {
                  if ( $('#elem').length &gt; 0 ) {
                      return true;
                  }
                  else {
                      attempt++;
  
                      if ( attempt &lt; 10 ) {
                          setTimeout(doEval,2000); //This seems iffy.
                      }
                      else {
                          return true;
                      }
                  }
              }
  
              return doEval();
          },cheerio)
  </code></pre>
  
  <p>So, wait and attempt again (upto a threshold), and if the element is not found, then just move on. The code seems wrong around setTimeout, because <code>.wait</code> is done at the browser-scope.</p>
  
  <p>Thanks in advance!</p>