I am trying to call an asynchronous function recursively using javascript promises but haven't found a pattern that works.
This is what I imagine would work:
var doAsyncThing = function(lastId){
  new Promise(function(resolve, reject){
    // async request with lastId
    return resolve(response)
  }
}
var recursivelyDoAsyncThing = function(lastId){
  doAsyncThing(lastId).then(function(response){
    return new Promise(function(resolve, reject){
      //do something with response
      if(response.hasMore){
        //get newlastId
        return resolve(recursivelyDoAsyncThing(newLastId));
      }else{
        resolve();
      }
    });
  });
}
recursivelyDoAsyncThing().then( function(){
  console.log('done');
});
Why doesn't this work? What have I misunderstood?
Is there a better pattern to solve this problem?
 
     
     
     
    