Class: EventMachine::Q::ResolvedPromise

Inherits:
Promise
  • Object
show all
Defined in:
lib/em-promise/q.rb

Instance Method Summary collapse

Constructor Details

#initialize(response, error = false) ⇒ ResolvedPromise

Returns a new instance of ResolvedPromise.

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
# File 'lib/em-promise/q.rb', line 95

def initialize(response, error = false)
	raise ArgumentError if error && response.is_a?(Promise)
	super()
	
	@error = error
	@response = response
end

Instance Method Details

#then(callback = nil, errback = nil, &blk) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/em-promise/q.rb', line 103

def then(callback = nil, errback = nil, &blk)
	result = Q.defer
	
	callback ||= blk
	
	EM.next_tick {
		if @error
			result.resolve(errback.nil? ? Q.reject(@response) : errback.call(@response))
		else
			result.resolve(callback.nil? ? @response : callback.call(@response))
		end
	}
	
	result.promise
end