~singpolyma/sgx-jmp

3a0f5bf8c8866793ffdc1c5762904f72b294d4f2 — Stephen Paul Weber 2 years ago d1f8b6a
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, 12 insertions(+), 0 deletions(-)

M lib/em.rb
M lib/em.rb => lib/em.rb +12 -0
@@ 12,4 12,16 @@ 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
	end
end