Skip to content

Instantly share code, notes, and snippets.

@kinnrot
Created August 27, 2017 06:57
Show Gist options
  • Save kinnrot/b311d7abe490e3f751412571c95f9769 to your computer and use it in GitHub Desktop.
Save kinnrot/b311d7abe490e3f751412571c95f9769 to your computer and use it in GitHub Desktop.
asynchronous awaited execution in event machine
def execute_awaited
fiber = Fiber.current
execute # run some non blocking code (return immidiatly)
self.callback do |response|
EventMachine.schedule do
fiber.resume response
end
end
self.errback do |err|
EventMachine.schedule do
fiber.resume err
end
end
result = Fiber.yield
if result.is_a?(Exception)
raise result
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment