Class: Whenner::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/whenner/callback.rb

Overview

A Callback is used internally by Deferred to store its callbacks. It provides the same call interface as regular blocks, but this will always return a promise for the block's return value.

When the block in question returns a regular object, a new deferred for that object is created and immediately fulfilled. When the block raises an exception, the returned promise is rejected with that exception. When the block returns a promise itself, the returned deferred will mimic that promise -- as if that promise is what actually was returned.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Callback

Returns a new instance of Callback.



21
22
23
24
# File 'lib/whenner/callback.rb', line 21

def initialize(block)
  @block    = block
  @deferred = Deferred.new
end

Instance Attribute Details

#block#call (readonly)

A callable object, usually a Ruby block.

Returns:



15
16
17
# File 'lib/whenner/callback.rb', line 15

def block
  @block
end

#deferredDeferred (readonly)

Returns the deferred object representing the block's return value.

Returns:

  • (Deferred)

    the deferred object representing the block's return value.



19
20
21
# File 'lib/whenner/callback.rb', line 19

def deferred
  @deferred
end

Instance Method Details

#call(*args) ⇒ Promise

Run the block, passing it any given arguments, and return a promise for its return value.

Returns:



30
31
32
33
# File 'lib/whenner/callback.rb', line 30

def call(*args)
  update_deferred(*args)
  deferred.promise
end

#promisePromise

Returns for this callback's #deferred.

Returns:

See Also:



37
38
39
# File 'lib/whenner/callback.rb', line 37

def promise
  deferred.promise
end