Javascript Generators -> Practical use case
Hi Guys, it's been a while since I didn't write anything, but today I want to share with you something I faced today, while I was reviewing the code of one of my teammates did, I saw a "good" implementation of how to deal with a paginated endpoint. Basically he used recursion in order to iterate the pages, something like this:
It works! but I don't like the recursion part, because if we need to iterate it we need some extra logic:
So far, this logic looks great, but I think it could be better, let's use a JS generator (I won't go deeply in the definition, but I'll tell you that a generator in JS is used to create iterators):
So far it's similar, but the advantages is that we let the generator to create an Iterator so it's pretty easy to iterate it, we could use a while, or a async for.
In summary, the generators are great options to deal with a paginated API, basically you transform your requests in iterators and then you have the control if there are more data or not. Of course you can use another options like recursion, both are great options but if you have the option of using generators, use them! you can let JS to worry about the edge cases, besides right now, they're supported in all the major browsers (Obviously I'm not taking in consideration IE).
So, that's it for today, I hope this use case has been useful. If you have any questions or concerns you can reach me through twitter or leave your comments here :) See you!
function handlePages(response) { const res = { data: response.data } if (response.pages.next_page) { res.next = async () => { const nextResponse = await request(response.pages.next_page) return handlePages(nextResponse) } } return res }
It works! but I don't like the recursion part, because if we need to iterate it we need some extra logic:
const response = await request(firstPageUrl) let page = handlePages(response) while(response.next) { // Complex logic here response = await response.next() }
So far, this logic looks great, but I think it could be better, let's use a JS generator (I won't go deeply in the definition, but I'll tell you that a generator in JS is used to create iterators):
async function* pagesGenerator(firstUrl) { let response = null do { response = await request(firstUrl) yield response.data } while(response.pages.next_page) }
So far it's similar, but the advantages is that we let the generator to create an Iterator so it's pretty easy to iterate it, we could use a while, or a async for.
<!-- Summary -->
In summary, the generators are great options to deal with a paginated API, basically you transform your requests in iterators and then you have the control if there are more data or not. Of course you can use another options like recursion, both are great options but if you have the option of using generators, use them! you can let JS to worry about the edge cases, besides right now, they're supported in all the major browsers (Obviously I'm not taking in consideration IE).
So, that's it for today, I hope this use case has been useful. If you have any questions or concerns you can reach me through twitter or leave your comments here :) See you!
Comments
Post a Comment