Created
August 10, 2015 16:58
-
-
Save soylent-grin/56ec935d44105f5d1b43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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