Promise version of a “while” loop?

删除回忆录丶 提交于 2019-12-22 12:45:14

问题


I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For instance:

var data = processData(JSON.parse(readFile(getFileName())));

Is the same as:

var data = getFileName()
    .then(readFile)
    .then(JSON.parse)
    .then(processData);

Now for my current use-case I want to write code to pull data from a massive public API. The API is paginated, so in a purely synchronous world I would write something like the following:

var data = [];
var offset = 0;
var total = 10000; // For example - not actually how this would work
while( offset < total ) {
    data.concat(getDataFromAPI(offset));
    offset = data.length;
}

Now my question is, how would I do this with promises? I could write something like:

var data = [];
var offset = 0;
var total = 10000;
getDataFromAPI(offset)
    .then(function(newData){
        data.concat(newData);
        return getDataFromAPI(data.length);
    });

But at this point I'm forced to just chain infinite .thens -- there's no looping logic. I feel like something should be possible using recursion, but I have no idea how to do it.

I'm using BluebirdJS as my promise library, so I have access to all of their helper methods.


回答1:


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
  });


来源:https://stackoverflow.com/questions/37552459/promise-version-of-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!