title: In javascript, a function which returns promise and retries the inner async process best practice
body: <p>I have a function which returns a javascript Promise and inside it there runs some asynchronously code. The async code, needs to be retried for couple of times in cases it fails. 
  I was doing that, till I observed some strange behaviors which made me wonder if I'm doing it right. So I had to change it. Both approaches snipped are down here.
  I have some idea why the first approach (asyncFunc) doesn't work, I would appreciate if someone could share some technical clarity about it.
  And for the second approach (ayncFunc_newer) any suggestion on how it can be done better?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var _retryCount = 0;
  
  // this is what I was doing
  function asyncFunc () {
   return new Promise(function(fulfill, reject) {
    doAsync()
     .then(fulfill)
     .catch(retry);
  
    function retry(promiseResult) {
     if(_retryCount &lt; 3) {
      _retryCount++;
      return asyncFunc();
     }
     else {
      reject(promiseResult);
     }
    }
   });
  }
  
  // this what I'm doing now
  function ayncFunc_newer() {
      return new Promise(function(fulfill, reject) {
          var retryCount = 0;
  
          doAsync()
              .then(fulfill)
              .catch(onReject);
  
          function onReject(bmAuthError) {
              if(retryCount &lt; 3) {
                  retryCount++;
                  logWarning(error);
                  
                  doAsync()
                   .then(fulfill)
                   .catch(onReject);
              }
              else {
                  fulfill(false);
              }
          }
      });                 
  };</code></pre>
  </div>
  </div>
  </p>