title: How to resolve recursive asynchronous promises?
body: <p>I'm playing around with promises and I'm having trouble with an asynchronous recursive promise. </p>
  
  <p>The scenario is an athlete starts running the 100m, I need to periodically check to see if they have finished and once they have finished, print their time.</p>
  
  <p><em>Edit to clarify</em> : </p>
  
  <p>In the real world the athlete is running on a server. <code>startRunning</code> involves making an ajax call to the server. <code>checkIsFinished</code> also involves making an ajax call to the server. The code below is an attempt to imitate that. The times and distances in the code are hardcoded in an attempt to keep things as simple as possible. Apologies for not being clearer.</p>
  
  <p><em>End edit</em></p>
  
  <p>I'd like to be able to write the following</p>
  
  <pre><code>startRunning()
    .then(checkIsFinished)
    .then(printTime)
    .catch(handleError)
  </code></pre>
  
  <p>where </p>
  
  <pre><code>var intervalID;
  var startRunning = function () {
    var athlete = {
      timeTaken: 0,
      distanceTravelled: 0
    };
    var updateAthlete = function () {
      athlete.distanceTravelled += 25;
      athlete.timeTaken += 2.5;
      console.log("updated athlete", athlete)
    }
  
    intervalID = setInterval(updateAthlete, 2500);
  
    return new Promise(function (resolve, reject) {
      setTimeout(resolve.bind(null, athlete), 2000);
    })
  };
  
  var checkIsFinished = function (athlete) {
    return new Promise(function (resolve, reject) {
      if (athlete.distanceTravelled &gt;= 100) {
        clearInterval(intervalID);
        console.log("finished");
        resolve(athlete);
  
      } else {
        console.log("not finished yet, check again in a bit");
        setTimeout(checkIsFinished.bind(null, athlete), 1000);
      }    
    });
  };
  
  var printTime = function (athlete) {
    console.log('printing time', athlete.timeTaken);
  };
  
  var handleError = function (e) { console.log(e); };
  </code></pre>
  
  <p>I can see that the promise that is created the first time <code>checkIsFinished</code> is never resolved. How can I ensure that that promise is resolved so that <code>printTime</code> is called?</p>
  
  <p>Instead of</p>
  
  <pre><code>resolve(athlete);
  </code></pre>
  
  <p>I could do</p>
  
  <pre><code>Promise.resolve(athlete).then(printTime);
  </code></pre>
  
  <p>But I'd like to avoid that if possible, I'd really like to be able to write</p>
  
  <pre><code>startRunning()
    .then(checkIsFinished)
    .then(printTime)
    .catch(handleError)
  </code></pre>