1. 2
    1. 1

      Generator are one of those things that I consider “programming voodoo”. I dont mean that as a negative, they just check off those boxes:

      • They can solve niche problems in a unique and sometimes better way
      • I dont understand how they work
      • I dont understand when they would be useful

      The only example ive come across as a “real world example” is merging a Map. You can do this already with spread syntax:

      new Map([...m1, ...m2]);

      or you can do it with a generator:

      new Map(function*() {
         yield* m1;
         yield* m2;

      the generator syntax is way more verbose, but it just looks interesting, like it has the potential to do some cool stuff in other cases.

      1. 1

        There is no need to fully understand generators to use the package, as you can see inside the TRY ME section. Anyway I suggest you to spend some time to study them if you don’t understand them. #ydkjs series of Kyle Simpson explain them very well

      2. 1

        Generators represent lazily computed collection of values. I’d suggest looking at several examples of problems they help solve elegantly: prolog interpreter that uses generators for yielding results, or a simpler variant of that use case: sudoku solver.

        Generators are also more “basic” than async functions (so that async functions can be implemented using generators) but because generators are more basic they can be used to abstract away whether an algorithm is async or sync (for example you select async version when working with files and streams and sync version when working with memory that is immediately available). See more details here.