~singpolyma/sgx-jmp

05a875390c5ed3d9bee77d9aa82377c738799801 — Stephen Paul Weber 1 year, 11 days ago 3550146
Helper to allow using sync-style code in a Promise context

This helper spins up a fiber and returns an unresolved EMPromise, then runs the
passed-in block inside the fiber and fulfills the promise with the result of the
block. Because nothing is looking for the Fiber to return it is free to act as a
trampoline for EMPromise#sync and other fiber-sync-style code that does not
block the EM reactor.
1 files changed, 13 insertions(+), 0 deletions(-)

M lib/em.rb
M lib/em.rb => lib/em.rb +13 -0
@@ 12,4 12,17 @@ module EM
		)
		promise
	end

	def self.promise_fiber
		promise = EMPromise.new
		Fiber.new {
			begin
				promise.fulfill(yield)
			rescue StandardError => e
				promise.reject(e)
			end
		}.resume
		promise
		promise
	end
end