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 .then
s -- 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.
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.