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.0'.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.
25 26 27 28 |
# File 'lib/promise.rb', line 25 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 |
.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
80 81 82 |
# File 'lib/promise.rb', line 80 def defer yield end |
#fulfill(value = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/promise.rb', line 61 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
34 35 36 |
# File 'lib/promise.rb', line 34 def fulfilled? state.equal?(:fulfilled) end |
#pending? ⇒ Boolean
30 31 32 |
# File 'lib/promise.rb', line 30 def pending? state.equal?(:pending) end |
#reject(reason = nil) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/promise.rb', line 73 def reject(reason = nil) dispatch do @state = :rejected @reason = reason_coercion(reason || Error) end end |
#rejected? ⇒ Boolean
38 39 40 |
# File 'lib/promise.rb', line 38 def rejected? state.equal?(:rejected) end |
#rescue(&block) ⇒ Object Also known as: catch
50 51 52 |
# File 'lib/promise.rb', line 50 def rescue(&block) self.then(nil, block) end |
#sync ⇒ Object
55 56 57 58 59 |
# File 'lib/promise.rb', line 55 def sync wait if pending? raise reason if rejected? value end |
#then(on_fulfill = nil, on_reject = nil, &block) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/promise.rb', line 42 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 |