Skip to content

Instantly share code, notes, and snippets.

@Voles
Created May 22, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Voles/e449db85cf172ba7916f4c9469b80713 to your computer and use it in GitHub Desktop.
Save Voles/e449db85cf172ba7916f4c9469b80713 to your computer and use it in GitHub Desktop.
Recursive Promise
function fetchResultsWithPagination(options, page, resultList) {
resultList = resultList || [];
page = page || 1;
return new Promise((resolve, reject) => {
makeHttpRequestPromise(Object.assign({}, options, { page: page }))
.then(payload => {
const newResultList = resultList.concat(payload);
if (payload.length >= options.per_page) {
return fetchResultsWithPagination(options, page + 1, newResultList);
} else {
return newResultList;
}
})
.then(resultList => {
resolve(resultList);
})
.catch(err => {
reject(err);
});
});
}
fetchResultsWithPagination()
.then(results => {
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment