Created
August 30, 2011 22:47
-
-
Save athoune/1182313 to your computer and use it in GitHub Desktop.
future the nth time
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
require 'eventmachine' | |
class NFuture | |
include EM::Deferrable | |
def initialize(times, &block) | |
@times = times | |
self.callback(&block) | |
end | |
def one_more_time(*arg) | |
@times -= 1 | |
self.succeed(*arg) if @times == 0 | |
end | |
end | |
n = NFuture.new 5 do | |
print "beuha" | |
EM.stop | |
end | |
EM.run do | |
(1..5).each do |i| | |
EM.add_timer(Random.rand(0.1)) do | |
puts i | |
n.one_more_time | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment