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 Method Summary collapse

Methods inherited from Promise

#catch, #finally, #progress

Constructor Details

#initialize(loop, defer) ⇒ DeferredPromise

Returns a new instance of DeferredPromise.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
# File 'lib/libuv/q.rb', line 84

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

Instance Method Details

#resolved?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/libuv/q.rb', line 150

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



99
100
101
102
103
104
105
106
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
# File 'lib/libuv/q.rb', line 99

def then(callback = nil, errback = nil, progback = nil, &blk)
    result = Q.defer(@loop)
    
    callback ||= blk
    
    wrappedCallback = proc { |val|
        begin
            result.resolve(callback.nil? ? val : callback.call(val))
        rescue Exception => e
            #warn "\nUnhandled exception: #{e.message}\n#{e.backtrace.join("\n")}\n"
            result.reject(e)
            @loop.log(:error, :q_resolve_cb, e)
        end
    }
    
    wrappedErrback = proc { |reason|
        begin
            result.resolve(errback.nil? ? Q.reject(@loop, reason) : errback.call(reason))
        rescue Exception => e
            #warn "Unhandled exception: #{e.message}\n#{e.backtrace.join("\n")}\n"
            result.reject(e)
            @loop.log(:error, :q_reject_cb, e)
        end
    }

    wrappedProgback = proc { |*progress|
        begin
            result.notify(progback.nil? ? progress : progback.call(*progress))
        rescue Exception => e
            #warn "Unhandled exception: #{e.message}\n#{e.backtrace.join("\n")}\n"
            @loop.log(:error, :q_progress_cb, e)
        end
    }
    
    #
    # Schedule as we are touching shared state
    #    Everything else is locally scoped
    #
    @loop.schedule do
        pending_array = pending
        
        if pending_array.nil?
            value.then(wrappedCallback, wrappedErrback, wrappedProgback)
        else
            pending_array << [wrappedCallback, wrappedErrback, wrappedProgback]
        end
    end
    
    result.promise
end