Home / Blog / My first generator (ES6)
Antonio's picture
Antonio Villareal
Comp. Scientist
21 Feb 2017
My
first generator (ES6)

Javascript ES6 generator. 

Body: 

Problem
I had to retrieve all the existing entries of a certain type from a
Contentful space. Contentful has a
pagination sistem; so the challenge was to get all of these pages
concurrently and wait until all were ready. It was a challenge because
doing this asynchronoulsy it is needed to know when all the pages were
retrieved and when the job is done to use the data later.

Solution
I wrote a generator to get all the pages, iterating over all the pages
until I get the total of the entries. In each iteration the code calls
contentful API to get one page asynchronously, (it waits with
'yield'). In each iteration it updates the counter 'skip' that counts
the number of retrieved items so far, it repeats this until we get to
the total of entries (this number is received in each iteration from
the API).

I used 'co' to check automatically if there is more data after each 'yield' call:

Conclusion
Even when this can be done with promises (in fact 'co'
uses them) the code turns simpler and looks more like a synchronous
code. The solution is based on the knowledge of the total number of
items without this the code will brake easily. Next time we can check
on this programatically.

Tags: