Class: Libuv::Q::ResolvedPromise

Inherits:
Promise
  • Object
show all
Defined in:
lib/libuv/q.rb

Constant Summary

Constants inherited from Promise

Promise::MAKE_PROMISE

Instance Method Summary collapse

Methods inherited from Promise

#catch, #finally, #progress

Constructor Details

#initialize(loop, response, error = false) ⇒ ResolvedPromise

Returns a new instance of ResolvedPromise.

Raises:

  • (ArgumentError)


168
169
170
171
172
173
174
175
# File 'lib/libuv/q.rb', line 168

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

Instance Method Details

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/libuv/q.rb', line 177

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