Skip to content

Instantly share code, notes, and snippets.

@didierfranc
Created September 5, 2018 08:22
Show Gist options
  • Save didierfranc/8308b51822bba757279c81aef5f6ce0e to your computer and use it in GitHub Desktop.
Save didierfranc/8308b51822bba757279c81aef5f6ce0e to your computer and use it in GitHub Desktop.
Use cursor with good old paginated web service
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