public
Created

node async foreach

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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);
};

JavaScript, Y U NO HAVE PATTERN MATCHING

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.