Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Created March 26, 2019 12:56
Show Gist options
  • Save hoodwink73/e232fbdc291565a78155ea48eff2a799 to your computer and use it in GitHub Desktop.
Save hoodwink73/e232fbdc291565a78155ea48eff2a799 to your computer and use it in GitHub Desktop.
Handling paginated API using Asyquence
const ASQ = require("asynquence")
const asyncTaskFactory = () => {
let invokationCount = 0
const asyncTask = () => {
return ASQ().then(done => {
setTimeout(() => {
invokationCount = invokationCount + 1
done({
data: "something",
hasNext: invokationCount < 3 ? true : false,
cursor: invokationCount * 10
})
}, 200)
})
}
return asyncTask
}
const asyncTask = asyncTaskFactory()
const getPaginatedAsyncData = (priorData = []) => {
return ASQ().then((done) => {
asyncTask().then((_, data) => {
if (data.hasNext) {
getPaginatedAsyncData([...priorData, data]).pipe(done)
} else {
done([...priorData, data])
}
})
})
}
getPaginatedAsyncData().val(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment