I am getting a list of droplets from a DigitalOcean API, but the list of droplets are per page.
The response give you the list of droplets on the page and the next page...
I am trying to get the next page of each promise recursively:
getDropletsPerPage(command,firstPage).then((response)=>{
nextPage= response['nextPage']
droplets= response['droplets']
getDropletsPerPage(command, nextPage).then((response)=>{
nextPage= response['nextPage']
droplets= response['droplets']
getDropletsPerPage(command, nextPage).then((response)=>{
nextPage= response['nextPage']
droplets= response['droplets']
// Repeat until last page..
})
})
})
You could use recursion:
const dispatcher = {
page: firstPage,
droplets: [],
execute: function () {
const self = this;
return new Promise(function (resolve, reject) {
getDropletsPerPage(command, this.page).then(function (response) {
self.page = response['nextPage'];
self.droplets = self.droplets.concat(response['droplets']);
if (nextPage === LAST_PAGE) {
resolve(true);/* done */
} else {
self.execute().then(function () {
resolve(true);
});
}
});
});
}
}
dispatcher.execute().then(function() {
/* reached last page */
});