Array.prototype.forEachDone = function(fn, scope, lastfn) {
for(var i = 0, c = 0, len = this.length; i < len; i++) {
fn.call(scope, this[i], i, this, function() {
++c === len && lastfn();
});
}
};
[1,2,3].forEachDone(function(num, i, arr, done){
setTimeout(function(){ console.log(num); done() }, 200);
}, this, function() {
console.log('done counting!');
});
this will do what you're looking for. forEachDone will take the exact same parameters as forEach, with an additional one at the end that is a callback to be called when the functions applied to each element of the array are done. the function that'll be applied to each element in the array also takes the exact same parameters as in forEach but also with an additional one that is a function that should be called when the function finishes.
PS: personally, i'd never touch a native object's prototype, but if you're looking for pretty, this is it.