title: JavaScript recursion returning a promise never resolving
body: <p>In JavaScript, I have an array of objects being some tasks to do. I iterate through this array with a <code>for</code> loop with <code>await</code>, calling a function <code>doOneTask</code> with returns a <code>Promise</code>.</p>
  
  <p>That works pretty well as long as the code inside <code>doOneTask</code> works as expected. However, those things often fail. Trying again helps almost all the time. So, I'd like to implement a procedure for auto-retrying inside the JavaScript code.
  My idea was a recursive function: In case of a failure, <code>doOneTask</code> call itself till the promise if finally resolved.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>var tasks = [{label: 'task0'},{label: 'task1'},{label: 'task2'}];
  async function mainFunction() {
      for(let k = 0; k &lt; tasks.length; k++) {
          await doOneTask(tasks[k]);
          console.log("doOneTask done for index " + k);
      }
      console.log("End reached!");
  }
  
  function doOneTask(task) {        
      return new Promise(async function (resolve,reject) {    
         console.log("Starting with: " + task.label);            
         let checkIfDoeSomeStuffWorked = await doSomeAsyncStuff();           
         if(checkIfDoeSomeStuffWorked == false) {
             console.log(task.label + ": FAILED");
             return doOneTask(task);
         }
         else {
             console.log(task.label + ": SUCCESS");
             resolve(true);
         }
      }); 
  }
  
  function doSomeAsyncStuff() {
      return new Promise(function (resolve,reject) {        
          var myRandom = Math.random();        
          if(myRandom &lt; 0.3) {
              resolve(true);
          }
          else {
              resolve(false);
          }        
      });
  }
  
  mainFunction();
  </code></pre>
  
  <p>(In real life, <code>doSomeAsyncStuff</code> is a backend call which often fails. The <code>random()</code> part is just for demonstration. In reality, I also limit the number of trials, before stopping the script.)</p>
  
  <p>However, it doesn't work. In case of a failure, the script stops after having reached the <code>SUCCESS</code> console log. I never get back to the loop and the next items in the loop never get executed.</p>