我从 DigitalOcean API 获取 Droplet 列表,但 Droplet 列表是每页的。
响应为您提供该页面和下一页上的水滴列表...
我正在尝试递归地获取每个 promise 的下一页:
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..
})
})
})
最佳答案
您可以使用递归:
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 */
});
关于javascript - 递归循环一个 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802388/