Class: Libuv::Q::ResolvedPromise

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

Constant Summary

Constants inherited from Promise

Promise::MAKE_PROMISE

Instance Attribute Summary

Attributes inherited from Promise

#trace

Instance Method Summary collapse

Methods inherited from Promise

#catch, #finally, #progress, #ruby_catch, #value

Constructor Details

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

Returns a new instance of ResolvedPromise.

Raises:

  • (ArgumentError)


179
180
181
182
183
184
185
186
# File 'lib/libuv/q.rb', line 179

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

    @reactor = reactor
    @error = error
    @response = response
end

Instance Method Details

#resolved?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/libuv/q.rb', line 214

def resolved?
    true
end

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/libuv/q.rb', line 188

def then(callback = nil, errback = nil, progback = nil, &blk)
    result = Q.defer(@reactor)

    callback ||= blk

    @reactor.next_tick {
        if @error
            begin
                result.resolve(errback.nil? ? Q.reject(@reactor, @response) : errback.call(@response))
            rescue Exception => e
                result.reject(e)
                @reactor.log e, 'performing promise rejection callback', @trace
            end
        else
            begin
                result.resolve(callback.nil? ? @response : callback.call(@response))
            rescue Exception => e
                result.reject(e)
                @reactor.log e, 'performing promise resolution callback', @trace
            end
        end
    }

    result.promise
end