Class: Restify::Promise

Inherits:
Concurrent::IVar
  • Object
show all
Defined in:
lib/restify/promise.rb

Defined Under Namespace

Classes: Writer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*dependencies, &task) ⇒ Promise

Returns a new instance of Promise.



6
7
8
9
10
11
# File 'lib/restify/promise.rb', line 6

def initialize(*dependencies, &task)
  @task         = task
  @dependencies = dependencies.flatten

  super(&nil)
end

Class Method Details

.create {|Writer.new(promise)| ... } ⇒ Object

Yields:



60
61
62
63
64
# File 'lib/restify/promise.rb', line 60

def create
  promise = Promise.new
  yield Writer.new(promise)
  promise
end

.fulfilled(value) ⇒ Object



66
67
68
69
70
# File 'lib/restify/promise.rb', line 66

def fulfilled(value)
  create do |writer|
    writer.fulfill value
  end
end

.rejected(value) ⇒ Object



72
73
74
75
76
# File 'lib/restify/promise.rb', line 72

def rejected(value)
  create do |writer|
    writer.reject value
  end
end

Instance Method Details

#execute(timeout = nil) ⇒ Object



27
28
29
# File 'lib/restify/promise.rb', line 27

def execute(timeout = nil)
  synchronize { ns_execute timeout }
end

#then(&block) ⇒ Object



23
24
25
# File 'lib/restify/promise.rb', line 23

def then(&block)
  Promise.new([self], &block)
end

#wait(timeout = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/restify/promise.rb', line 13

def wait(timeout = nil)
  t = Timeout.new(timeout, self)

  execute(t) if pending?

  super
  raise t if incomplete?
  self
end