Skip to content

Instantly share code, notes, and snippets.

@athoune
Created August 30, 2011 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athoune/1182313 to your computer and use it in GitHub Desktop.
Save athoune/1182313 to your computer and use it in GitHub Desktop.
future the nth time
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