
Untitled
By: a guest on
Aug 18th, 2012 | syntax:
None | size: 0.71 KB | hits: 9 | expires: Never
def http_get( url )
# get the fiber our browser request is being processed in
calling_fiber = Fiber.current
# make an async request
# EventMachine queues the http.callback block and executes it at some point after the http request returns
http = EventMachine::HttpRequest.new( url ).get
http.callback { calling_fiber.resume( http ) }
# the calling_fiber yields control back to the EM reactor, which can continue processing other browser requests etc
# When the async http request returns, the callback block is queued for EM to execute when it received control again.
# The callback resumes the calling fiber, and the get method returns the result of the http get request.
return calling_fiber.yield
end