Created
September 5, 2018 08:22
-
-
Save didierfranc/8308b51822bba757279c81aef5f6ce0e to your computer and use it in GitHub Desktop.
Use cursor with good old paginated web service
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 get = require("./api") | |
const getAfter = async (limit, after) => { | |
const vindex = parseInt(after.split(":")[0]) | |
const page = Math.floor(vindex / limit) + 1 | |
const modulo = vindex % limit | |
const setId = page => (id, index) => { | |
const vindex = (page - 1) * limit + index + 1 | |
return { id: `${vindex}:${id}` } | |
} | |
const format = (page, arg) => e => e.map(setId(page)).slice(...arg) | |
const list = [[page, limit, [modulo]], [page + 1, limit, [0, modulo]]] | |
const [r1, r2] = await Promise.all( | |
list.map(([p, l, s]) => get(p, l).then(format(p, s))) | |
) | |
return [...r1, ...r2] | |
} | |
;(async () => { | |
const r = await getAfter(3, "11:11") | |
console.log(r) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment