Created
April 1, 2012 01:55
-
-
Save MatthewMueller-Snippets/2270511 to your computer and use it in GitHub Desktop.
Super simple synchronous forEach loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.forEachSync = function(fn, done) { | |
if(!this.length) return done(null); | |
arr = this.reverse(); | |
function next(err) { | |
if(err) return done(err); | |
var elem = arr.pop(); | |
if(!elem) return done(err); | |
return fn(elem, next); | |
} | |
return fn(arr.pop(), next); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment