Skip to content

Instantly share code, notes, and snippets.

@joubertnel
Created January 11, 2012 22:22
Show Gist options
  • Save joubertnel/1597138 to your computer and use it in GitHub Desktop.
Save joubertnel/1597138 to your computer and use it in GitHub Desktop.
pmap: function(operation, mapping, then) {
var ret = new Array(this.length);
var remainingWork = this.length;
this.forEach(function(x, idx, i) {
operation.call(null, x, function() {
var extendedArgs = Array.prototype.slice.call(arguments);
var mappedVal;
extendedArgs.push(x);
mappedVal = mapping.apply(null, extendedArgs);
ret[idx] = mappedVal;
remainingWork = remainingWork - 1;
if ((remainingWork === 0) && then) then.call(null, ret);
});
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment