Skip to content

Instantly share code, notes, and snippets.

@myrne
Last active December 17, 2015 22:19
Show Gist options
  • Save myrne/5680851 to your computer and use it in GitHub Desktop.
Save myrne/5680851 to your computer and use it in GitHub Desktop.
Old faithful.eachSeries implementation.
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