我正在使用一个返回分页数据的函数。它最多返回100个项目和一个用于检索下一个100个项目的键。我想检索所有可用的项目。
我该如何递归实现这一点?在这里使用递归是一个好选择吗?我能否不使用递归以其他方式完成?
我正在使用 Bluebird 3x 作为 promises 库。
以下是我尝试实现的代码片段:
我该如何递归实现这一点?在这里使用递归是一个好选择吗?我能否不使用递归以其他方式完成?
我正在使用 Bluebird 3x 作为 promises 库。
以下是我尝试实现的代码片段:
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) { ... }
),我该如何将这些 promise 链接起来,以便最终获得所有端点列表等。