TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JavaScript iterator patterns

104 pointsby loigeover 6 years ago

6 comments

anonytraryover 6 years ago
One thing I dislike about the iterator protocol is that it creates garbage around every value you&#x27;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); &#x2F;&#x2F; {value: 1, done: false} &#x2F;&#x2F; {value: 2, done: false} &#x2F;&#x2F; {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) &#x2F;&#x2F; 1 &#x2F;&#x2F; 2 &#x2F;&#x2F; 3 </code></pre> Not that this is too much of an issue, I&#x27;m sure using yield&#x2F;yield*&#x2F;of will eventually optimize this.
评论 #18961046 未加载
评论 #18959203 未加载
评论 #18960525 未加载
评论 #18961229 未加载
jonduboisover 6 years ago
Iterators and generators are powerful and can make code much cleaner&#x2F;more declarative. Especially when used with async&#x2F;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:&#x2F;&#x2F;hackernoon.com&#x2F;getting-started-with-asyngular-bbe3dd1c716c" rel="nofollow">https:&#x2F;&#x2F;hackernoon.com&#x2F;getting-started-with-asyngular-bbe3dd...</a><p>Feedback welcome.
评论 #18962195 未加载
guest124678over 6 years ago
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 &#x2F; 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.
评论 #18959112 未加载
评论 #18960689 未加载
评论 #18960089 未加载
sktrdieover 6 years ago
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:&#x2F;&#x2F;lmatteis.github.io&#x2F;react-behavioral&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lmatteis.github.io&#x2F;react-behavioral&#x2F;</a>
评论 #18960593 未加载
_xgwover 6 years ago
&gt; Consecutive calls to next() will always produce { done: true }.<p>Not necessarily. You can make recursive generators which never &quot;terminate&quot;. For example, here&#x27;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>
评论 #19012588 未加载
nicknasoover 6 years ago
Good article thanks.
评论 #18960748 未加载