module Concurrent::Promises::FactoryMethods

Container of all {Future}, {Event} factory methods. They are never constructed directly with new.

Public Instance Methods

any(*futures_and_or_events)
Alias for: any_resolved_future
any_event(*futures_and_or_events) click to toggle source

@!macro promises.shortcut.on @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 317
def any_event(*futures_and_or_events)
  any_event_on default_executor, *futures_and_or_events
end
any_event_on(default_executor, *futures_and_or_events) click to toggle source

Creates new event which becomes resolved after first of the futures_and_or_events resolves. @!macro promises.any-touch

@!macro promises.param.default_executor @param [AbstractEventFuture] futures_and_or_events @return [Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 327
def any_event_on(default_executor, *futures_and_or_events)
  AnyResolvedEventPromise.new_blocked_by(futures_and_or_events, default_executor).event
end
any_fulfilled_future(*futures_and_or_events) click to toggle source

@!macro promises.shortcut.on @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 298
def any_fulfilled_future(*futures_and_or_events)
  any_fulfilled_future_on default_executor, *futures_and_or_events
end
any_fulfilled_future_on(default_executor, *futures_and_or_events) click to toggle source

Creates new future which is resolved after first of futures_and_or_events is fulfilled. Its result equals result of the first resolved future or if all futures_and_or_events reject, it has reason of the last resolved future. @!macro promises.any-touch @!macro promises.event-conversion

@!macro promises.param.default_executor @param [AbstractEventFuture] futures_and_or_events @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 311
def any_fulfilled_future_on(default_executor, *futures_and_or_events)
  AnyFulfilledFuturePromise.new_blocked_by(futures_and_or_events, default_executor).future
end
any_resolved_future(*futures_and_or_events) click to toggle source

@!macro promises.shortcut.on @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 276
def any_resolved_future(*futures_and_or_events)
  any_resolved_future_on default_executor, *futures_and_or_events
end
Also aliased as: any
any_resolved_future_on(default_executor, *futures_and_or_events) click to toggle source

Creates new future which is resolved after first futures_and_or_events is resolved. Its result equals result of the first resolved future. @!macro promises.any-touch

If resolved it does not propagate {Concurrent::AbstractEventFuture#touch}, leaving delayed
futures un-executed if they are not required any more.

@!macro promises.event-conversion

@!macro promises.param.default_executor @param [AbstractEventFuture] futures_and_or_events @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 292
def any_resolved_future_on(default_executor, *futures_and_or_events)
  AnyResolvedFuturePromise.new_blocked_by(futures_and_or_events, default_executor).future
end
delay(*args, &task) click to toggle source

@!macro promises.shortcut.on @return [Future, Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 188
def delay(*args, &task)
  delay_on default_executor, *args, &task
end
delay_on(default_executor, *args, &task) click to toggle source

Creates new event or future which is resolved only after it is touched, see {Concurrent::AbstractEventFuture#touch}.

@!macro promises.param.default_executor @overload delay_on(default_executor, *args, &task)

If task is provided it returns a {Future} representing the result of the task.
@!macro promises.param.args
@yield [*args] to the task.
@!macro promise.param.task-future
@return [Future]

@overload delay_on(default_executor)

If no task is provided, it returns an {Event}
@return [Event]
# File lib/concurrent-ruby/concurrent/promises.rb, line 205
def delay_on(default_executor, *args, &task)
  event = DelayPromise.new(default_executor).event
  task ? event.chain(*args, &task) : event
end
fulfilled_future(value, default_executor = self.default_executor) click to toggle source

Creates resolved future with will be fulfilled with the given value.

@!macro promises.param.default_executor @param [Object] value @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 125
def fulfilled_future(value, default_executor = self.default_executor)
  resolved_future true, value, nil, default_executor
end
future(*args, &task) click to toggle source

@!macro promises.shortcut.on @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 92
def future(*args, &task)
  future_on(default_executor, *args, &task)
end
future_on(default_executor, *args, &task) click to toggle source

Constructs new Future which will be resolved after block is evaluated on default executor. Evaluation begins immediately.

@!macro promises.param.default_executor @!macro promises.param.args @yield [*args] to the task. @!macro promise.param.task-future @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 104
def future_on(default_executor, *args, &task)
  ImmediateEventPromise.new(default_executor).future.then(*args, &task)
end
make_future(argument = nil, default_executor = self.default_executor) click to toggle source

General constructor. Behaves differently based on the argument's type. It's provided for convenience but it's better to be explicit.

@see rejected_future, resolved_event, fulfilled_future @!macro promises.param.default_executor @return [Event, Future]

@overload make_future(nil, default_executor = self.default_executor)

@param [nil] nil
@return [Event] resolved event.

@overload make_future(a_future, default_executor = self.default_executor)

@param [Future] a_future
@return [Future] a future which will be resolved when a_future is.

@overload make_future(an_event, default_executor = self.default_executor)

@param [Event] an_event
@return [Event] an event which will be resolved when an_event is.

@overload make_future(exception, default_executor = self.default_executor)

@param [Exception] exception
@return [Future] a rejected future with the exception as its reason.

@overload make_future(value, default_executor = self.default_executor)

@param [Object] value when none of the above overloads fits
@return [Future] a fulfilled future with the value.
# File lib/concurrent-ruby/concurrent/promises.rb, line 172
def make_future(argument = nil, default_executor = self.default_executor)
  case argument
  when AbstractEventFuture
    # returning wrapper would change nothing
    argument
  when Exception
    rejected_future argument, default_executor
  when nil
    resolved_event default_executor
  else
    fulfilled_future argument, default_executor
  end
end
rejected_future(reason, default_executor = self.default_executor) click to toggle source

Creates resolved future with will be rejected with the given reason.

@!macro promises.param.default_executor @param [Object] reason @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 134
def rejected_future(reason, default_executor = self.default_executor)
  resolved_future false, nil, reason, default_executor
end
resolvable_event() click to toggle source

@!macro promises.shortcut.on @return [ResolvableEvent]

# File lib/concurrent-ruby/concurrent/promises.rb, line 61
def resolvable_event
  resolvable_event_on default_executor
end
resolvable_event_on(default_executor = self.default_executor) click to toggle source

Created resolvable event, user is responsible for resolving the event once by {Promises::ResolvableEvent#resolve}.

@!macro promises.param.default_executor @return [ResolvableEvent]

# File lib/concurrent-ruby/concurrent/promises.rb, line 70
def resolvable_event_on(default_executor = self.default_executor)
  ResolvableEventPromise.new(default_executor).future
end
resolvable_future() click to toggle source

@!macro promises.shortcut.on @return [ResolvableFuture]

# File lib/concurrent-ruby/concurrent/promises.rb, line 76
def resolvable_future
  resolvable_future_on default_executor
end
resolvable_future_on(default_executor = self.default_executor) click to toggle source

Creates resolvable future, user is responsible for resolving the future once by {Promises::ResolvableFuture#resolve}, {Promises::ResolvableFuture#fulfill}, or {Promises::ResolvableFuture#reject}

@!macro promises.param.default_executor @return [ResolvableFuture]

# File lib/concurrent-ruby/concurrent/promises.rb, line 86
def resolvable_future_on(default_executor = self.default_executor)
  ResolvableFuturePromise.new(default_executor).future
end
resolved_event(default_executor = self.default_executor) click to toggle source

Creates resolved event.

@!macro promises.param.default_executor @return [Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 142
def resolved_event(default_executor = self.default_executor)
  ImmediateEventPromise.new(default_executor).event
end
resolved_future(fulfilled, value, reason, default_executor = self.default_executor) click to toggle source

Creates resolved future with will be either fulfilled with the given value or rejection with the given reason.

@param [true, false] fulfilled @param [Object] value @param [Object] reason @!macro promises.param.default_executor @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 116
def resolved_future(fulfilled, value, reason, default_executor = self.default_executor)
  ImmediateFuturePromise.new(default_executor, fulfilled, value, reason).future
end
schedule(intended_time, *args, &task) click to toggle source

@!macro promises.shortcut.on @return [Future, Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 212
def schedule(intended_time, *args, &task)
  schedule_on default_executor, intended_time, *args, &task
end
schedule_on(default_executor, intended_time, *args, &task) click to toggle source

Creates new event or future which is resolved in intended_time.

@!macro promises.param.default_executor @!macro promises.param.intended_time

@param [Numeric, Time] intended_time `Numeric` means to run in `intended_time` seconds.
  `Time` means to run on `intended_time`.

@overload schedule_on(default_executor, intended_time, *args, &task)

If task is provided it returns a {Future} representing the result of the task.
@!macro promises.param.args
@yield [*args] to the task.
@!macro promise.param.task-future
@return [Future]

@overload schedule_on(default_executor, intended_time)

If no task is provided, it returns an {Event}
@return [Event]
# File lib/concurrent-ruby/concurrent/promises.rb, line 231
def schedule_on(default_executor, intended_time, *args, &task)
  event = ScheduledPromise.new(default_executor, intended_time).event
  task ? event.chain(*args, &task) : event
end
zip(*futures_and_or_events)
Alias for: zip_futures
zip_events(*futures_and_or_events) click to toggle source

@!macro promises.shortcut.on @return [Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 260
def zip_events(*futures_and_or_events)
  zip_events_on default_executor, *futures_and_or_events
end
zip_events_on(default_executor, *futures_and_or_events) click to toggle source

Creates new event which is resolved after all futures_and_or_events are resolved. (Future is resolved when fulfilled or rejected.)

@!macro promises.param.default_executor @param [AbstractEventFuture] futures_and_or_events @return [Event]

# File lib/concurrent-ruby/concurrent/promises.rb, line 270
def zip_events_on(default_executor, *futures_and_or_events)
  ZipEventsPromise.new_blocked_by(futures_and_or_events, default_executor).event
end
zip_futures(*futures_and_or_events) click to toggle source

@!macro promises.shortcut.on @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 238
def zip_futures(*futures_and_or_events)
  zip_futures_on default_executor, *futures_and_or_events
end
Also aliased as: zip
zip_futures_on(default_executor, *futures_and_or_events) click to toggle source

Creates new future which is resolved after all futures_and_or_events are resolved. Its value is array of zipped future values. Its reason is array of reasons for rejection. If there is an error it rejects. @!macro promises.event-conversion

If event is supplied, which does not have value and can be only resolved, it's
represented as `:fulfilled` with value `nil`.

@!macro promises.param.default_executor @param [AbstractEventFuture] futures_and_or_events @return [Future]

# File lib/concurrent-ruby/concurrent/promises.rb, line 252
def zip_futures_on(default_executor, *futures_and_or_events)
  ZipFuturesPromise.new_blocked_by(futures_and_or_events, default_executor).future
end