TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Iterating over a Javascript array at a consistent rate

2 点作者 jmontgomery超过 11 年前

1 comment

acjohnson55超过 11 年前
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 未加载