Created
October 8, 2012 13:10
-
-
Save pH200/3852437 to your computer and use it in GitHub Desktop.
node async foreach
This file contains hidden or 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.forEachAsync = function (callback, complete) { | |
var raiseComplete/*: (arg?: bool) => void*/ = complete ? (function (arg) { | |
complete(arg); | |
}) : (function () { }); | |
function next (self, index, length) { /* rec */ | |
if (index === length) { | |
raiseComplete(true); | |
} else { | |
process.nextTick(function () { | |
function closureNext () { | |
next(self, index + 1, length); | |
} | |
var flow = callback(self[index], index, self, closureNext); | |
if (flow === false) { | |
raiseComplete(false); | |
} else if (flow === true) { | |
return; // closure next | |
} else { | |
closureNext(); | |
} | |
}); | |
} | |
} | |
next(Object(this), 0, this.length); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JavaScript, Y U NO HAVE PATTERN MATCHING