Skip to content

Instantly share code, notes, and snippets.

@soylent-grin
Created August 10, 2015 16:58
Show Gist options
  • Save soylent-grin/56ec935d44105f5d1b43 to your computer and use it in GitHub Desktop.
Save soylent-grin/56ec935d44105f5d1b43 to your computer and use it in GitHub Desktop.
var asyncReduce = function(array, iterator, callback) {
var index = 0;
if (!(array instanceof Array)) {
throw new Error('first argument is required to be an array');
}
if (!iterator || typeof iterator !== "function") {
throw new Error('second argument required to be a function');
}
var previousValue;
(function innerIterator() {
previousValue = array[++index];
if (index < array.length - 1) {
previousValue = iterator(previousValue, array[index], index, innerIterator.bind(this));
} else {
return callback && callback(previousValue);
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment