Class: Q::Promise
- Inherits:
-
Object
- Object
- Q::Promise
- Defined in:
- lib/q/promise.rb
Instance Method Summary collapse
- #always(&block) ⇒ Object
- #fail(&block) ⇒ Object
- #fulfill(type, result) ⇒ Object
-
#initialize(defer) ⇒ Promise
constructor
A new instance of Promise.
- #then(&block) ⇒ Object
Constructor Details
#initialize(defer) ⇒ Promise
Returns a new instance of Promise.
2 3 4 5 6 7 8 |
# File 'lib/q/promise.rb', line 2 def initialize(defer) @defer = defer @thens = [] @fails = [] @alwayses = [] end |
Instance Method Details
#always(&block) ⇒ Object
18 19 20 |
# File 'lib/q/promise.rb', line 18 def always(&block) enqueue_and_call(@alwayses, block, :finished?) end |
#fail(&block) ⇒ Object
14 15 16 |
# File 'lib/q/promise.rb', line 14 def fail(&block) enqueue_and_call(@fails, block, :rejected?) end |
#fulfill(type, result) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/q/promise.rb', line 22 def fulfill(type, result) @result = result if type == :resolved @thens.each { |callback| callback.call(@result) } elsif type == :rejected @fails.each { |callback| callback.call(@result) } end @alwayses.each { |callback| callback.call(@result) } end |
#then(&block) ⇒ Object
10 11 12 |
# File 'lib/q/promise.rb', line 10 def then(&block) enqueue_and_call(@thens, block, :resolved?) end |