@@ -172,29 +172,34 @@ class Promise
172172
173173 # Initialize a new Promise with the provided options.
174174 #
175- # @param [Hash] opts the options used to define the behavior at update and deref
175+ # @!macro [attach] promise_init_options
176176 #
177- # @option opts [Promise] :parent the parent `Promise` when building a chain/tree
178- # @option opts [Proc] :on_fulfill fulfillment handler
179- # @option opts [Proc] :on_reject rejection handler
177+ # @param [Hash] opts the options used to define the behavior at update and deref
180178 #
181- # @option opts [Boolean] :operation (false) when `true` will execute the future on the global
182- # operation pool (for long-running operations), when `false` will execute the future on the
183- # global task pool (for short-running tasks)
184- # @option opts [object] :executor when provided will run all operations on
185- # this executor rather than the global thread pool (overrides :operation)
179+ # @option opts [Promise] :parent the parent `Promise` when building a chain/tree
180+ # @option opts [Proc] :on_fulfill fulfillment handler
181+ # @option opts [Proc] :on_reject rejection handler
186182 #
187- # @option opts [String] :dup_on_deref (false) call `#dup` before returning the data
188- # @option opts [String] :freeze_on_deref (false) call `#freeze` before returning the data
189- # @option opts [String] :copy_on_deref (nil) call the given `Proc` passing the internal value and
190- # returning the value returned from the proc
183+ # @option opts [Boolean] :operation (false) when `true` will execute the future on the global
184+ # operation pool (for long-running operations), when `false` will execute the future on the
185+ # global task pool (for short-running tasks)
186+ # @option opts [object] :executor when provided will run all operations on
187+ # this executor rather than the global thread pool (overrides :operation)
188+ # @option opts [object, Array] :args zero or more arguments to be passed the task block on execution
189+ #
190+ # @option opts [String] :dup_on_deref (false) call `#dup` before returning the data
191+ # @option opts [String] :freeze_on_deref (false) call `#freeze` before returning the data
192+ # @option opts [String] :copy_on_deref (nil) call the given `Proc` passing the internal value and
193+ # returning the value returned from the proc
191194 #
192195 # @see http://wiki.commonjs.org/wiki/Promises/A
193196 # @see http://promises-aplus.github.io/promises-spec/
194197 def initialize ( opts = { } , &block )
195198 opts . delete_if { |k , v | v . nil? }
196199
197200 @executor = OptionsParser ::get_executor_from ( opts ) || Concurrent . configuration . global_operation_pool
201+ @args = OptionsParser ::get_arguments_from ( opts )
202+
198203 @parent = opts . fetch ( :parent ) { nil }
199204 @on_fulfill = opts . fetch ( :on_fulfill ) { Proc . new { |result | result } }
200205 @on_reject = opts . fetch ( :on_reject ) { Proc . new { |reason | raise reason } }
@@ -219,7 +224,6 @@ def self.reject(reason, opts = {})
219224 end
220225
221226 # @return [Promise]
222- # @since 0.5.0
223227 def execute
224228 if root?
225229 if compare_and_set_state ( :pending , :unscheduled )
@@ -232,7 +236,18 @@ def execute
232236 self
233237 end
234238
235- # @since 0.5.0
239+ # Create a new `Promise` object with the given block, execute it, and return the
240+ # `:pending` object.
241+ #
242+ # @!macro promise_init_options
243+ #
244+ # @return [Promise] the newly created `Promise` in the `:pending` state
245+ #
246+ # @raise [ArgumentError] if no block is given
247+ #
248+ # @example
249+ # promise = Concurrent::Promise.execute{ sleep(1); 42 }
250+ # promise.state #=> :pending
236251 def self . execute ( opts = { } , &block )
237252 new ( opts , &block ) . execute
238253 end
@@ -389,6 +404,7 @@ def self.aggregate(method, *promises)
389404 composite
390405 end
391406
407+ # @!visibility private
392408 def set_pending
393409 mutex . synchronize do
394410 @state = :pending
@@ -413,6 +429,7 @@ def on_reject(reason)
413429 nil
414430 end
415431
432+ # @!visibility private
416433 def notify_child ( child )
417434 if_state ( :fulfilled ) { child . on_fulfill ( apply_deref_options ( @value ) ) }
418435 if_state ( :rejected ) { child . on_reject ( @reason ) }
@@ -421,7 +438,7 @@ def notify_child(child)
421438 # @!visibility private
422439 def realize ( task )
423440 @executor . post do
424- success , value , reason = SafeTaskExecutor . new ( task ) . execute
441+ success , value , reason = SafeTaskExecutor . new ( task ) . execute ( * @args )
425442
426443 children_to_notify = mutex . synchronize do
427444 set_state! ( success , value , reason )
@@ -432,11 +449,13 @@ def realize(task)
432449 end
433450 end
434451
452+ # @!visibility private
435453 def set_state! ( success , value , reason )
436454 set_state ( success , value , reason )
437455 event . set
438456 end
439457
458+ # @!visibility private
440459 def synchronized_set_state! ( success , value , reason )
441460 mutex . lock
442461 set_state! ( success , value , reason )
0 commit comments