Skip to content

Instantly share code, notes, and snippets.

@pH200
Created October 8, 2012 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pH200/3852437 to your computer and use it in GitHub Desktop.
Save pH200/3852437 to your computer and use it in GitHub Desktop.
node async foreach
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);
};
@pH200
Copy link
Author

pH200 commented Oct 8, 2012

JavaScript, Y U NO HAVE PATTERN MATCHING

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment