DEV Community

Discussion on: A Quick, Practical Use Case for ES6 Generators: Building an Infinitely Repeating Array

Collapse
 
tunaxor profile image
Angel Daniel Munoz Gonzalez

Cool article! thanks for the nice read, I always wanted a use case for generators but I was never able to decide when to use them (some times I keep this idea) but I remember usin Generators to retrieve paginated mongo cursors from a collection with a registry over hundreds of thousand records, we had to process each record individually (at least that was what I was tasked to do), but you know expecting things to be faster than the blink of an eye.

something like this (pardon me about syntax errors or not being logic at all, the original snippet is somewhere and I do remember do further modifications to it)

function* getCursorList(count, size, collection) {
  const batchList = [];
  let i = 0;
  let n = count;
  while (i < n) {
    yield collection.find().clone().limit(size).skip(i += size);
  }
}
const cursors = getCursorList(count, 10, collection);
for (const cursor of cursors) {
    //blablabla
}

and then processed each cursor on a stream for given collection, generators were my salvation because they were lazily evaluated once the previous stream finished processing the whole cursor, that also prevented my streams from clogging and losing data. I tried a similar solution without the generators, but I was losing data and my streams were hanging too