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



28
29
30
# File 'lib/libuv/q.rb', line 28

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



45
46
47
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
# File 'lib/libuv/q.rb', line 45

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



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

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