Promise version of a “while” loop?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 04:51:47

I feel like something should be possible using recursion

Exactly. You can name the callback so you can reference it again. As long as the condition isn't met, return a promise from the callback. Otherwise return the final result:

getDataFromAPI(offset)
  .then(function next(newData){
    data.concat(newData);
    var newOffset = data.length;
    return newOffset < total ? getDataFromAPI(newOffset).then(next) : data;
  })
  .then(function(data) {
    console.log(data); // final result
  });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!