Class: Promise
- Inherits:
-
Object
- Object
- Promise
- Includes:
- Progress
- Defined in:
- lib/promise.rb,
lib/promise/group.rb,
lib/promise/version.rb,
lib/promise/callback.rb,
lib/promise/progress.rb
Defined Under Namespace
Modules: Progress
Constant Summary collapse
- Error =
Class.new(RuntimeError)
- VERSION =
'0.7.1'.freeze
Instance Attribute Summary collapse
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #defer ⇒ Object
- #fulfill(value = nil) ⇒ Object
- #fulfilled? ⇒ Boolean
-
#initialize ⇒ Promise
constructor
A new instance of Promise.
- #pending? ⇒ Boolean
- #reject(reason = nil) ⇒ Object
- #rejected? ⇒ Boolean
- #rescue(&block) ⇒ Object (also: #catch)
- #sync ⇒ Object
- #then(on_fulfill = nil, on_reject = nil, &block) ⇒ Object
Methods included from Progress
Constructor Details
#initialize ⇒ Promise
Returns a new instance of Promise.
33 34 35 36 |
# File 'lib/promise.rb', line 33 def initialize @state = :pending @callbacks = [] end |
Instance Attribute Details
#reason ⇒ Object (readonly)
Returns the value of attribute reason.
14 15 16 |
# File 'lib/promise.rb', line 14 def reason @reason end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
14 15 16 |
# File 'lib/promise.rb', line 14 def state @state end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
14 15 16 |
# File 'lib/promise.rb', line 14 def value @value end |
Class Method Details
.all(enumerable) ⇒ Object
21 22 23 |
# File 'lib/promise.rb', line 21 def self.all(enumerable) Group.new(new, enumerable).promise end |
.map_value(obj) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/promise.rb', line 25 def self.map_value(obj) if obj.is_a?(Promise) obj.then { |value| yield value } else yield obj end end |
.resolve(obj) ⇒ Object
16 17 18 19 |
# File 'lib/promise.rb', line 16 def self.resolve(obj) return obj if obj.is_a?(self) new.tap { |promise| promise.fulfill(obj) } end |
Instance Method Details
#defer ⇒ Object
88 89 90 |
# File 'lib/promise.rb', line 88 def defer yield end |
#fulfill(value = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/promise.rb', line 69 def fulfill(value = nil) if Promise === value Callback.assume_state(value, self) else dispatch do @state = :fulfilled @value = value end end nil end |
#fulfilled? ⇒ Boolean
42 43 44 |
# File 'lib/promise.rb', line 42 def fulfilled? state.equal?(:fulfilled) end |
#pending? ⇒ Boolean
38 39 40 |
# File 'lib/promise.rb', line 38 def pending? state.equal?(:pending) end |
#reject(reason = nil) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/promise.rb', line 81 def reject(reason = nil) dispatch do @state = :rejected @reason = reason_coercion(reason || Error) end end |
#rejected? ⇒ Boolean
46 47 48 |
# File 'lib/promise.rb', line 46 def rejected? state.equal?(:rejected) end |
#rescue(&block) ⇒ Object Also known as: catch
58 59 60 |
# File 'lib/promise.rb', line 58 def rescue(&block) self.then(nil, block) end |
#sync ⇒ Object
63 64 65 66 67 |
# File 'lib/promise.rb', line 63 def sync wait if pending? raise reason if rejected? value end |
#then(on_fulfill = nil, on_reject = nil, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/promise.rb', line 50 def then(on_fulfill = nil, on_reject = nil, &block) on_fulfill ||= block next_promise = self.class.new add_callback(Callback.new(self, on_fulfill, on_reject, next_promise)) next_promise end |