title: Node.js: Why is my expected value not returned in 'catch' block of 'try-catch' clause?
body: <p>I have a Node.js project which tests the functionality of a website.  It utilizes Webdriver.io v4 and Mocha/Chai.</p>
  
  <p>I've created a function that checks if an element exists on a page with a timeout of 1 minute.  If the element exists, it should return <code>true</code>.  If it does not, it should return <code>false</code>.</p>
  
  <p>I use the same function to test if an element is NOT present on the page.  In this case, I am expecting the function to return <code>false</code>.  However, instead of returning false, the function throws a Timeout error and returns neither <code>true</code> or <code>false</code>.  This is strange because I've included a <code>return false</code> statement in the catch block of my try-catch clause.</p>
  
  <p>In this project, when a function fails, I will get a message such as <code>expected false to equal true</code> or <code>expected undefined to equal true</code>. In this case, I get the message <code>Timeout of 60000ms exceeded. Try to reduce the run time or increase your timeout for test specs (http://webdriver.io/guide/testrunner/timeouts.html); if returning a Promise, ensure it resolves.</code></p>
  
  <p>Yes, I am expecting the <code>element.waitForExist()</code> to throw a Timeout error, but this error should be handled in the catch block by returning <code>false</code>.  The program does display the error log as expected by the <code>console.log(ex)</code> line, but does not return <code>false</code>.</p>
  
  <p>Why is my function not returning <code>false</code> in this case?  What would be the best/easiest way to return the correct value? Thank you!</p>
  
  <p>This is my function:</p>
  
  <pre><code>checkElementExists: {
          value: function (element) {
              try {
                  element.waitForExist();
                  if (element.isExisting()) {
                      return true;
                  } else {
                      return false;
                  }
              } catch (ex) {
                  console.log(ex);
                  return false;
              }
          }
      }
  </code></pre>
  
  <p>Expected: If element exists on page, the function returns <code>true</code>.  If the element does not exist on the page, the function returns <code>false</code>.</p>
  
  <p>Actual: If element exists on page, the function returns <code>true</code>.  If the element does not exist on the page, a Timeout error is thrown but neither <code>true</code> or <code>false</code> is returned.</p>