title: Exit from function after resolving protractor promise
body: <p>I am fairly new to JavaScript and Protractor. I have simple task in my test that I am unable to complete.</p>
  
  <ol>
  <li>Check on available Tab on web page.</li>
  <li>Check if element is visible on web page.</li>
  <li>a) If Yes, <code>return deffered.fullfil(true)</code><br>
  b) If No,<br />
  &nbsp; &nbsp; &nbsp; - Click on In-Progress Tab <br/>
  &nbsp; &nbsp; &nbsp; - Click on Available Tab.<br />
  &nbsp; &nbsp; &nbsp; - Go to Step 1.</li>
  </ol>
  
  <p>I am trying to do this recursively and below is my code. It is printing Element found but never exits the function after that and times out.</p>
  
  <pre><code>var check_availability = function(counter, totalCount, element){
  var deferred = potractor.promise.defer()
  if(counter &lt;= totalCount){
      browser.wait(function(){
          browser.wait(EC.visibilityOf(element),2000)
          return element
      }).then(function(success){
          console.log('Element found.')
          return deferred.fulfill(true)
      }, function(err){
         inprogressTab.click()
         .then(() =&gt; availableTab .click())
         .then(() =&gt; check_availability (counter+1 , totalCount, element))
      })
     } else{
     return deferred.reject(false)
    }
    return deferred.promise
  }
  </code></pre>
  
  <p>PS: This is a sample code that I am using, corrected some spelling mistakes and syntax.</p>