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

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(&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



22
23
24
# File 'lib/libuv/q.rb', line 22

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

#finallyPromise

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



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

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

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

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

#progress(&blk) ⇒ Object



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

def progress(&blk)
    self.then(nil, nil, 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



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

def value
    ::Libuv.co(self)
end