Method: Concurrent::Promise#initialize

Defined in:
lib/concurrent/promise.rb

#initialize(opts = {}, &block) ⇒ Promise

Initialize a new Promise with the provided options.

Parameters:

  • opts (Hash) (defaults to: {})

    the options used to define the behavior at update and deref

Options Hash (opts):

  • :parent (Promise)

    the parent Promise when building a chain/tree

  • :on_fulfill (Proc)

    fulfillment handler

  • :on_reject (Proc)

    rejection handler

  • :operation (Boolean) — default: false

    when true will execute the future on the global operation pool (for long-running operations), when false will execute the future on the global task pool (for short-running tasks)

  • :executor (object)

    when provided will run all operations on this executor rather than the global thread pool (overrides :operation)

  • :dup_on_deref (String) — default: false

    call #dup before returning the data

  • :freeze_on_deref (String) — default: false

    call #freeze before returning the data

  • :copy_on_deref (String) — default: nil

    call the given Proc passing the internal value and returning the value returned from the proc

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/concurrent/promise.rb', line 34

def initialize(opts = {}, &block)
  opts.delete_if { |k, v| v.nil? }

  @executor = OptionsParser::get_executor_from(opts) || Concurrent.configuration.global_operation_pool
  @parent = opts.fetch(:parent) { nil }
  @on_fulfill = opts.fetch(:on_fulfill) { Proc.new { |result| result } }
  @on_reject = opts.fetch(:on_reject) { Proc.new { |reason| raise reason } }

  @promise_body = block || Proc.new { |result| result }
  @state = :unscheduled
  @children = []

  init_obligation
end