Last active
October 31, 2017 11:58
-
-
Save framp/8e74dfa713049be1ca6b70e8a3557659 to your computer and use it in GitHub Desktop.
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
| const assert = require('assert') | |
| const fakeFetch = (url, page=+url.split('page=')[1], | |
| perpage=3, | |
| data=[1,4,6,9,7,3,1,4]) => | |
| Promise.resolve({ | |
| data: data.slice((page-1)*perpage, page*perpage), | |
| nextPage: page*perpage < data.length ? page+1 : null | |
| }) | |
| const fetchPages = (resultsArray, page, end) => | |
| fakeFetch(`https://my-api.com/?page=${page}`) | |
| .then(({ data, nextPage }) => | |
| (nextPage !== null && nextPage <= end) | |
| ? fetchPages(resultsArray.concat(data), nextPage, end) | |
| : resultsArray.concat(data)) | |
| fetchPages([], 1, 3) | |
| .then(results => assert.deepEqual(results, [1,4,6,9,7,3,1,4])) | |
| .catch(err => assert.equal(err, null)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment