Skip to content

Instantly share code, notes, and snippets.

@jj1bdx
Forked from anonymous/t.rb
Created June 2, 2017 03:05
Show Gist options
  • Save jj1bdx/0e1a7c5b7bf4f3c857eeb3628eaee693 to your computer and use it in GitHub Desktop.
Save jj1bdx/0e1a7c5b7bf4f3c857eeb3628eaee693 to your computer and use it in GitHub Desktop.
def async(name, &b)
define_method name do |*args|
f = Fiber.new{
r = b.call(*args)
f.instance_variable_set(:@result, r)
}
def f.value
while true
unless self.alive?
break @result
else
self.resume
end
end
end
f
end
end
require 'fiber'
def await task
while true
r = task.resume
if task.alive?
Fiber.yield
else
break r
end
end
end
def do_something1
end
def do_something2
end
async :task2 do
do_something1
Fiber.yield
do_something2
end
async :task1 do |name|
await task2()
"hello #{name}"
end
task1_instance = task1("ko1")
p task1_instance.value
@jj1bdx
Copy link
Author

jj1bdx commented Jun 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment