Class: MrDarcy::Promise::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mr_darcy/promise/base.rb

Overview

An abstract superclass for all promise implementations.

Direct Known Subclasses

Celluloid, EM, Synchronous, Thread

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Base

Create a new promise and schedule it for execution.



13
14
15
16
17
18
19
# File 'lib/mr_darcy/promise/base.rb', line 13

def initialize block
  state
  schedule_promise do
    evaluate_promise &block
  end
  did_initialize
end

Instance Method Details

#fail(&block) ⇒ Object

Create a new promise that rejects and calls the supplied block when this promise is rejected.



32
33
34
35
36
37
# File 'lib/mr_darcy/promise/base.rb', line 32

def fail &block
  ensure_child_promise
  child_promise.reject_block = block
  resolve_or_reject_child_as_needed
  child_promise.promise
end

#finalObject

Wait until the promise is resolved or rejected and return self.



46
47
48
# File 'lib/mr_darcy/promise/base.rb', line 46

def final
  Kernel::raise "Subclasses must implement me"
end

#raiseObject

Wait until the promise is resolver or rejected, and if rejected raise the error value in this context.



52
53
54
55
56
57
58
59
60
61
# File 'lib/mr_darcy/promise/base.rb', line 52

def raise
  r = result
  if rejected?
    if r.is_a? Exception
      super r
    else
      super RuntimeError, r
    end
  end
end

#reject(exception) ⇒ Object

Reject this promise with the provided error/value.



70
71
72
73
# File 'lib/mr_darcy/promise/base.rb', line 70

def reject exception
  do_reject exception
  self
end

#resolve(value) ⇒ Object

Resolve this promise with the provided value.



64
65
66
67
# File 'lib/mr_darcy/promise/base.rb', line 64

def resolve value
  do_resolve value
  self
end

#resultObject

Wait until the promise is resolved or rejected and return it's result value.



41
42
43
# File 'lib/mr_darcy/promise/base.rb', line 41

def result
  Kernel::raise "Subclasses must implement me"
end

#then(&block) ⇒ Object

Create a new promise that resolves and calls the supplied block when this promise is resolved.



23
24
25
26
27
28
# File 'lib/mr_darcy/promise/base.rb', line 23

def then &block
  ensure_child_promise
  child_promise.resolve_block = block
  resolve_or_reject_child_as_needed
  child_promise.promise
end