class Async::WorkerPool::Promise

Execute the given work in a background thread.

def call

Execute the work and resolve the promise.
def call
	work = nil
	
	@guard.synchronize do
		@thread = ::Thread.current
		
		return unless work = @work
	end
	
	resolve(work.call)
rescue Exception => error
	reject(error)
end

def cancel

Cancel the work and raise an exception in the background thread.
def cancel
	return unless @work
	
	@guard.synchronize do
		@work = nil
		@state = :cancelled
		@thread&.raise(Interrupt)
	end
end

def initialize(work)

@parameter work [Proc] The work to be done.

Create a new promise.
def initialize(work)
	@work = work
	@state = :pending
	@value = nil
	@guard = ::Mutex.new
	@condition = ::ConditionVariable.new
	@thread = nil
end

def reject(error)

def reject(error)
synchronize do
= nil
d = nil
 = error
 = :failed
tion.broadcast

def resolve(value)

def resolve(value)
synchronize do
= nil
d = nil
 = value
 = :resolved
tion.broadcast

def wait

@returns [Object] The result of the work.

Wait for the work to be done.
def wait
	@guard.synchronize do
		while @state == :pending
			@condition.wait(@guard)
		end
		
		if @state == :failed
			raise @value
		else
			return @value
		end
	end
end