Created
April 7, 2014 12:27
-
-
Save alyssais/10019357 to your computer and use it in GitHub Desktop.
A Ruby implementation of a waterfall control flow thingy.
This file contains hidden or 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
module Async | |
class << self | |
def waterfall(objects, handler, &block) | |
enumerator = objects.each | |
prev_result = nil | |
run_next = lambda do | |
begin | |
# onwards! | |
prev_result = handler.(enumerator.next, prev_result) do |success| | |
success ? run_next.call : block.(false) | |
end | |
rescue StopIteration | |
# done! | |
block.(true) | |
end | |
end | |
run_next.call | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment