

Iterating over a Javascript array at a consistent rate - jmontgomery
http://blog.jackmontgomery.io/iterating-over-a-js-array-at-a-consistent-rate/

======
acjohnson55
That's cool, but you'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:

    
    
      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));
      }
    

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't want to presume anything but the bare `when`. If
you really get used to programming "the combinator way", 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é ([https://leanpub.com/javascript-
allonge/read](https://leanpub.com/javascript-allonge/read)), which makes this
all look far more appealing than I've done here.

~~~
jmontgomery
That seems like an interesting way of doing it as well. I've used promises
before but not enough to be comfortable with this yet. Thanks for the tip!

~~~
acjohnson55
Nevermind, that doesn't work. I didn't even use my `delay` function. I was
trying to get too cute. But you might see what I was getting at.

If you want to really understand promises, this is a good resource:
[https://gist.github.com/domenic/3889970](https://gist.github.com/domenic/3889970)

