Created
January 6, 2019 11:58
-
-
Save adrianalonso/ed30bcd615e881bf65c5b8536020f1f0 to your computer and use it in GitHub Desktop.
Pagination Promise Chain
This file contains hidden or 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 getPaginatedElements(getResourcePromise, progressFn, limit = 25, offset = 0, elements = []) { | |
return new Promise((resolve, reject) => | |
getResourcePromise({limit, offset}) | |
.then(response => { | |
const newElements = elements.concat(response.records); | |
if (response.records.length === 0) { | |
resolve(newElements); | |
} else { | |
progressFn && progressFn(elements); | |
getPaginatedElements(getResourcePromise, progressFn, limit, offset+limit, newElements) | |
.then(resolve) | |
.catch(reject) | |
} | |
}).catch(reject)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment