Class: Libuv::Q::DeferredPromise

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

Overview

A new promise instance is created when a deferred instance is created and can be retrieved by calling deferred.promise

Direct Known Subclasses

Dns, File, Handle, Work

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, defer) ⇒ DeferredPromise

Returns a new instance of DeferredPromise.

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# File 'lib/libuv/q.rb', line 94

def initialize(reactor, defer)
    raise ArgumentError unless defer.is_a?(Deferred)
    super()
    
    @reactor = reactor
    @defer = defer
end

Instance Method Details

#resolved?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/libuv/q.rb', line 157

def resolved?
    pending.nil?
end

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

regardless of when the promise was or will be resolved / rejected, calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument, the result or rejection reason.

Parameters:

  • callbacks (Proc, Proc, Proc, &blk)

    error, success, progress, success_block

Returns:

  • (Promise)

    Returns an unresolved promise for chaining



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/libuv/q.rb', line 109

def then(callback = nil, errback = nil, progback = nil, &blk)
    result = Q.defer(@reactor)
    
    callback ||= blk
    
    wrappedCallback = proc { |val|
        begin
            result.resolve(callback.nil? ? val : callback.call(val))
        rescue Exception => e
            result.reject(e)
            @reactor.log e, 'performing promise resolution callback', @trace
        end
    }
    
    wrappedErrback = proc { |reason|
        begin
            result.resolve(errback.nil? ? Q.reject(@reactor, reason) : errback.call(reason))
        rescue Exception => e
            result.reject(e)
            @reactor.log e, 'performing promise rejection callback', @trace
        end
    }

    wrappedProgback = proc { |*progress|
        begin
            result.notify(progback.nil? ? progress : progback.call(*progress))
        rescue Exception => e
            @reactor.log e, 'performing promise progress callback', @trace
        end
    }
    
    #
    # Schedule as we are touching shared state
    #    Everything else is locally scoped
    #
    @reactor.schedule do
        pending_array = pending
        
        if pending_array.nil?
            reference.then(wrappedCallback, wrappedErrback, wrappedProgback)
        else
            pending_array << [wrappedCallback, wrappedErrback, wrappedProgback]
        end
    end
    
    result.promise
end