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.

Iterating over a Javascript array at a consistent rate

2 pointsby jmontgomeryover 11 years ago

1 comment

acjohnson55over 11 years ago
That&#x27;s cool, but you&#x27;re kind of mixing together a few different pieces of functionality. To me, this kind of thing would be much more readable using promises to wrap the asynchronous nature of the code. Assuming you used when.js, you could do something like:<p><pre><code> function delay(fn, ms) { return when(function (resolve) { setTimeout(function () { fn(); resolve(); }, ms || 0); }); } function eachDelay(arr, fn, ms) { arr.reduce(function (prev, curr) { prev.then(function () { return fn(curr); }); }, when(null)); } </code></pre> That looks more complicated on the surface, but in reality, libraries like when.js and underscore give you convenience functions that abstract away much of this boilerplate. I didn&#x27;t want to presume anything but the bare `when`. If you really get used to programming &quot;the combinator way&quot;, you get to the point where you write these cute little primative combinators that can be combined in very readable ways. If you like coding in this style, definitely check out Javascript Allongé (<a href="https://leanpub.com/javascript-allonge/read" rel="nofollow">https:&#x2F;&#x2F;leanpub.com&#x2F;javascript-allonge&#x2F;read</a>), which makes this all look far more appealing than I&#x27;ve done here.
评论 #6765829 未加载