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

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)


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

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

    @reactor = reactor
    @defer = defer
end

Instance Method Details

#resolved?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/libuv/q.rb', line 154

def resolved?
    pending.nil?
end

#then(callback = nil, errback = nil, progback = nil) ⇒ 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



107
108
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
# File 'lib/libuv/q.rb', line 107

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

    wrappedProgback = proc { |*progress|
        begin
            result.notify(progback ? progback.call(*progress) : 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