Last active
December 17, 2015 22:19
-
-
Save myrne/5680851 to your computer and use it in GitHub Desktop.
Old faithful.eachSeries implementation.
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
makePromise = require "make-promise" | |
module.exports = eachSeries = (values, iterator, options ={}) -> | |
i = 0 | |
makePromise (cb) -> | |
resolver = (i) -> | |
(result) -> | |
options.handleResult? result, i | |
iterate() | |
iterate = -> | |
if (i >= values.length) or options.stopEarly?() | |
cb null, options.getFinalValue?() | |
else | |
try promise = iterator values[i] | |
catch err then return cb err | |
try promise.then(resolver(i)).then null, (err) -> cb err | |
catch error then cb error | |
i++ | |
iterate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment