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, loop|
    result = Q.defer(loop)
    if (resolved)
        result.resolve(value)
    else
        result.reject(value)
    end
    result.promise
}.freeze

Instance Method Summary collapse

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



31
32
33
# File 'lib/libuv/q.rb', line 31

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libuv/q.rb', line 48

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

    handleCallback = lambda {|value, isResolved|
        callbackOutput = nil
        begin
            callbackOutput = callback.call
        rescue Exception => e
            @loop.log(:error, :q_finally_cb, e)
            return MAKE_PROMISE.call(e, false, @loop)
        end

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

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

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



36
37
38
# File 'lib/libuv/q.rb', line 36

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

#ruby_catchObject

This allows subclasses to make use of the catch feature



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

alias_method :ruby_catch, :catch