【发布时间】:2016-05-10 09:54:07
【问题描述】:
我正在使用一个以分页形式返回数据的函数。所以它将返回最多 100 个项目和一个检索下 100 个项目的键。我想检索所有可用的项目。
我如何递归地实现这一点?递归在这里是一个不错的选择吗?我可以用其他方法不递归吗?
我使用 Bluebird 3x 作为 Promise 库。
这是我想要实现的目标:
getEndpoints(null, platformApplication)
.then(function(allEndpoints) {
// process on allEndpoints
});
function getEndpoints(nextToken, platformApplication) {
var params = {
PlatformApplicationArn: platformApplication
};
if (nextToken) {
params.NextToken = nextToken;
}
return sns.listEndpointsByPlatformApplicationAsync(params)
.then(function(data) {
if (data.NextToken) {
// There is more data available that I want to retrieve.
// But the problem here is that getEndpoints return a promise
// and not the array. How do I chain this here so that
// in the end I get an array of all the endpoints concatenated.
var moreEndpoints = getEndpoints(data.NextToken, platformApplication);
moreEndpoints.push.apply(data.Endpoints, moreEndpoints);
}
return data.Endpoints;
});
}
但问题是,如果要检索更多数据(请参阅if (data.NextToken) { ... }),我如何将承诺链接起来,以便最终获得所有端点的列表等。
【问题讨论】:
-
你想什么时候获得下一个 100?是什么触发的?
-
当有更多可用数据时。如果 data.NextToken 可用,这意味着更多数据可用,所以我必须得到它。
-
所以您要获取所有端点?
-
是的。抱歉,这个问题不清楚,更新了。
标签: javascript node.js pagination promise bluebird