One thing I dislike about the iterator protocol is that it creates garbage around every value you're iterating over:<p><pre><code> let set = new Set([1,2,3]), it = set.values(), n;
while(!(n = it.next()).done) console.log(n);
// {value: 1, done: false}
// {value: 2, done: false}
// {value: 3, done: false}
</code></pre>
I found this strange considering javascript has symbols now. Symbols would be perfect for the iterator protocol:<p><pre><code> while((n = it.next()) !== Symbol.iteratorDone) console.log(n)
// 1
// 2
// 3
</code></pre>
Not that this is too much of an issue, I'm sure using yield/yield*/of will eventually optimize this.
Iterators and generators are powerful and can make code much cleaner/more declarative. Especially when used with async/await.<p>If anyone is interested, I recently released a real-time WebSocket library (Node.js and front end) which only uses Async Iterators to stream data (no event listener callbacks):<p><a href="https://hackernoon.com/getting-started-with-asyngular-bbe3dd1c716c" rel="nofollow">https://hackernoon.com/getting-started-with-asyngular-bbe3dd...</a><p>Feedback welcome.
The next step I would have shown if I wrote this article, would have been to tell reader that JS generators as they are now, do not work with async / wait.<p>As long as you have some simple Fibonacci interview question they look nice to show off, but when you want to use them for real like return some paginated data from server as on going iterator, then current JS generators cannot be used.<p>This makes js generators limited for any real usage, given JS IO is async.
If anybody is interested in coding in a way that is closer to requirements, generators are really helpful to switch towards a paradigm called Behavioral Programming: <a href="https://lmatteis.github.io/react-behavioral/" rel="nofollow">https://lmatteis.github.io/react-behavioral/</a>
> Consecutive calls to next() will always produce { done: true }.<p>Not necessarily. You can make recursive generators which never "terminate". For example, here's a codegolfed version of a recursive generator which represents a n + 1 sequence:<p><pre><code> p=function*a(x){yield x;yield*a(x+1)}(0)</code></pre>