Imagine you are developing an application utilizing generators. And they are getting big and contain duplicate code.
When I started to develop my own project with widely generators usage, complexity came not so late. So I tried first to write shared generator and call it:
* iteratorIns() { //... const iterator = this.search(key, true); while (true) { const iteration = iterator.next(); if (iteration.done) { foundAt = iteration.value; break; } yield iteration.value; } //... }
Here I iterate through my dedicated generator serach
using while cycle. The main point here is to pass returned value from nested to parent generator at line 10 and grab the result at line 7.
Now we can throw this bunch of code away and replace it by super expression: yield*
. Rewrite previous code:
* iteratorIns() { //... foundAt = yield* this.search(key, true); //... }
Voila! Here our iteratorIns
will iterate through serach
generator and store the result in foundAt
value in one line code!