Created
May 22, 2018 20:59
-
-
Save Voles/e449db85cf172ba7916f4c9469b80713 to your computer and use it in GitHub Desktop.
Recursive Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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