class Concurrent::Promises::ResolvableFuture

A Future which can be resolved by user.

Public Instance Methods

evaluate_to(*args, &block) click to toggle source

Evaluates the block and sets its result as future's value fulfilling, if the block raises an exception the future rejects with it.

@yield [*args] to the block. @yieldreturn [Object] value @return [self]

# File lib/concurrent-ruby/concurrent/promises.rb, line 1384
def evaluate_to(*args, &block)
  promise.evaluate_to(*args, block)
end
evaluate_to!(*args, &block) click to toggle source

Evaluates the block and sets its result as future's value fulfilling, if the block raises an exception the future rejects with it.

@yield [*args] to the block. @yieldreturn [Object] value @return [self] @raise [Exception] also raise reason on rejection.

# File lib/concurrent-ruby/concurrent/promises.rb, line 1395
def evaluate_to!(*args, &block)
  promise.evaluate_to(*args, block).wait!
end
fulfill(value, raise_on_reassign = true, reserved = false) click to toggle source

Makes the future fulfilled with `value`, which triggers all dependent futures.

@param [Object] value @!macro promise.param.raise_on_reassign @!macro promise.param.reserved

# File lib/concurrent-ruby/concurrent/promises.rb, line 1364
def fulfill(value, raise_on_reassign = true, reserved = false)
  resolve_with Fulfilled.new(value), raise_on_reassign, reserved
end
reason(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {Future#reason} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [Exception, timeout_value, nil] @see Future#reason

# File lib/concurrent-ruby/concurrent/promises.rb, line 1492
def reason(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.reason
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        return internal_state.reason
      end
    end
    timeout_value
  end
end
reject(reason, raise_on_reassign = true, reserved = false) click to toggle source

Makes the future rejected with `reason`, which triggers all dependent futures.

@param [Object] reason @!macro promise.param.raise_on_reassign @!macro promise.param.reserved

# File lib/concurrent-ruby/concurrent/promises.rb, line 1374
def reject(reason, raise_on_reassign = true, reserved = false)
  resolve_with Rejected.new(reason), raise_on_reassign, reserved
end
resolve(fulfilled = true, value = nil, reason = nil, raise_on_reassign = true, reserved = false) click to toggle source

Makes the future resolved with result of triplet `fulfilled?`, `value`, `reason`, which triggers all dependent futures.

@param [true, false] fulfilled @param [Object] value @param [Object] reason @!macro promise.param.raise_on_reassign @!macro promise.param.reserved

# File lib/concurrent-ruby/concurrent/promises.rb, line 1354
def resolve(fulfilled = true, value = nil, reason = nil, raise_on_reassign = true, reserved = false)
  resolve_with(fulfilled ? Fulfilled.new(value) : Rejected.new(reason), raise_on_reassign, reserved)
end
result(timeout = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {Future#result} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [::Array(Boolean, Object, Exception), nil] @see Future#result

# File lib/concurrent-ruby/concurrent/promises.rb, line 1513
def result(timeout = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.result
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        internal_state.result
      end
    end
    # otherwise returns nil
  end
end
value(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {Future#value} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [Object, timeout_value, nil] @see Future#value

# File lib/concurrent-ruby/concurrent/promises.rb, line 1448
def value(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved timeout
    internal_state.value
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        return internal_state.value
      end
    end
    timeout_value
  end
end
value!(timeout = nil, timeout_value = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {Future#value!} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [Object, timeout_value, nil] @raise [Exception] {#reason} on rejection @see Future#value!

# File lib/concurrent-ruby/concurrent/promises.rb, line 1470
def value!(timeout = nil, timeout_value = nil, resolve_on_timeout = nil)
  if wait_until_resolved! timeout
    internal_state.value
  else
    if resolve_on_timeout
      unless resolve(*resolve_on_timeout, false)
        # if it fails to resolve it was resolved in the meantime
        # so return value as if there was no timeout
        raise self if rejected?
        return internal_state.value
      end
    end
    timeout_value
  end
end
wait(timeout = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {AbstractEventFuture#wait} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [self, true, false] @see AbstractEventFuture#wait

# File lib/concurrent-ruby/concurrent/promises.rb, line 1410
def wait(timeout = nil, resolve_on_timeout = nil)
  super(timeout) or if resolve_on_timeout
                      # if it fails to resolve it was resolved in the meantime
                      # so return true as if there was no timeout
                      !resolve(*resolve_on_timeout, false)
                    else
                      false
                    end
end
wait!(timeout = nil, resolve_on_timeout = nil) click to toggle source

Behaves as {Future#wait!} but has one additional optional argument resolve_on_timeout.

@!macro promises.resolvable.resolve_on_timeout @return [self, true, false] @raise [Exception] {#reason} on rejection @see Future#wait!

Calls superclass method Concurrent::Promises::Future#wait!
# File lib/concurrent-ruby/concurrent/promises.rb, line 1427
def wait!(timeout = nil, resolve_on_timeout = nil)
  super(timeout) or if resolve_on_timeout
                      if resolve(*resolve_on_timeout, false)
                        false
                      else
                        # if it fails to resolve it was resolved in the meantime
                        # so return true as if there was no timeout
                        raise self if rejected?
                        true
                      end
                    else
                      false
                    end
end
with_hidden_resolvable() click to toggle source

Creates new future wrapping receiver, effectively hiding the resolve method and similar.

@return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 1531
def with_hidden_resolvable
  @with_hidden_resolvable ||= FutureWrapperPromise.new_blocked_by1(self, @DefaultExecutor).future
end