Class: Libuv::Q::Promise Abstract

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

Overview

This class is abstract.

Direct Known Subclasses

DeferredPromise, ResolvedPromise

Constant Summary collapse

MAKE_PROMISE =

Used by finally method

proc { |value, resolved, reactor|
    result = Q.defer(reactor)
    if (resolved)
        result.resolve(value)
    else
        result.reject(value)
    end
    result.promise
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#traceObject

Allows a backtrace to be included in any errors



14
15
16
# File 'lib/libuv/q.rb', line 14

def trace
  @trace
end

Instance Method Details

#catch(callback = nil, &blk) ⇒ Promise

regardless of when the promise was or will be resolved / rejected, calls the error callback asynchronously if the promise is rejected.

Parameters:

  • callbacks (Proc, &blk)

    error, error_block

Returns:

  • (Promise)

    Returns an unresolved promise for chaining



34
35
36
# File 'lib/libuv/q.rb', line 34

def catch(callback = nil, &blk)
    self.then(nil, callback || blk)
end

#finally(callback = nil, &blk) ⇒ Promise

allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved.

Parameters:

  • callbacks (Proc, &blk)

    finally, finally_block

Returns:

  • (Promise)

    Returns an unresolved promise for chaining



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/libuv/q.rb', line 55

def finally(callback = nil, &blk)
    callback ||= blk

    handleCallback = lambda {|value, isResolved|
        callbackOutput = nil
        begin
            callbackOutput = callback.call
        rescue Exception => e
            @reactor.log e, 'performing promise finally callback', @trace
            return MAKE_PROMISE.call(e, false, @reactor)
        end

        if callbackOutput.is_a?(Promise)
            return callbackOutput.then(proc {
                    MAKE_PROMISE.call(value, isResolved, @reactor)
                }, proc { |err|
                    MAKE_PROMISE.call(err, false, @reactor)
                })
        else
            return MAKE_PROMISE.call(value, isResolved, @reactor)
        end
    }

    self.then(proc {|val|
        handleCallback.call(val, true)
    }, proc{|err|
        handleCallback.call(err, false)
    })
end

#progress(callback = nil, &blk) ⇒ Object



39
40
41
# File 'lib/libuv/q.rb', line 39

def progress(callback = nil, &blk)
    self.then(nil, nil, callback || blk)
end

#ruby_catchObject

This allows subclasses to make use of the catch feature



11
# File 'lib/libuv/q.rb', line 11

alias_method :ruby_catch, :catch

#valueObject

A future that provides the value or raises an error if a rejection occurs



44
45
46
# File 'lib/libuv/q.rb', line 44

def value
    co self
end