約束事でページ付けされたAPIを呼びたいと思います。利用可能なページ数の情報は、最初は分かっていませんが、すべての応答で配信されます。具体的には、私はJiraのsearchを呼び出しています、ページング情報の一部は次のようになります。私は再帰とページネーションの取り扱いを解決約束事でページ付けされたAPIを呼び出す
{
"startAt": 0,
"maxResults": 15,
"total": 100000,
...
}
、これは活字体で私のソリューションです:
search(jql: string, fields: string[] = [], maxResults = 15) : Promise<JiraIssue[]> {
// container for the issues
let issues: Array<JiraIssue> = new Array<JiraIssue>();
// define recursive function to collect the issues per page and
// perform a recursive call to fetch the next page
let recursiveFn = (jql: string, fields: string[], startAt: number, maxResults: number) :
Promise<JiraIssue[]> => {
return this
// retrieves one page
.searchPage(jql, fields, startAt, maxResults)
.then((searchResult: JiraSearchResult) => {
// saves the result of the retrieved page
issues.push.apply(issues, searchResult.issues);
if (searchResult.startAt + searchResult.maxResults < searchResult.total) {
// retrieves the next page with a recursive call
return recursiveFn(jql, fields,
searchResult.startAt + searchResult.maxResults,
maxResults);
}
else {
// returns the collected issues
return issues;
}
})
};
// and execute it
return recursiveFn(jql, fields, 0, maxResults);
}
しかし、I再帰的なアプローチが気に入らないのは、小さな結果セットでのみうまくいくからです(私はスタックオーバーフローを恐れています)。どのように再帰的なアプローチでこの問題を解決しますか?
これは実際の再帰ではなく、関数がその中で呼び出されているため、スタックオーバーフローの危険はありません。 –