@@ -197,6 +197,8 @@ class Promise < IVar
197
197
# @option opts [object, Array] :args zero or more arguments to be passed
198
198
# the task block on execution
199
199
#
200
+ # @yield The block operation to be performed asynchronously.
201
+ #
200
202
# @raise [ArgumentError] if no block is given
201
203
#
202
204
# @see http://wiki.commonjs.org/wiki/Promises/A
@@ -217,17 +219,37 @@ def initialize(opts = {}, &block)
217
219
@children = [ ]
218
220
end
219
221
220
- # @return [Promise]
222
+ # Create a new `Promise` and fulfill it immediately.
223
+ #
224
+ # @!macro executor_and_deref_options
225
+ #
226
+ # @!macro promise_init_options
227
+ #
228
+ # @raise [ArgumentError] if no block is given
229
+ #
230
+ # @return [Promise] the newly created `Promise`
221
231
def self . fulfill ( value , opts = { } )
222
232
Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , true , value , nil ) }
223
233
end
224
234
225
- # @return [Promise]
235
+ # Create a new `Promise` and reject it immediately.
236
+ #
237
+ # @!macro executor_and_deref_options
238
+ #
239
+ # @!macro promise_init_options
240
+ #
241
+ # @raise [ArgumentError] if no block is given
242
+ #
243
+ # @return [Promise] the newly created `Promise`
226
244
def self . reject ( reason , opts = { } )
227
245
Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , false , nil , reason ) }
228
246
end
229
247
230
- # @return [Promise]
248
+ # Execute an `:unscheduled` `Promise`. Immediately sets the state to `:pending` and
249
+ # passes the block to a new thread/thread pool for eventual execution.
250
+ # Does nothing if the `Promise` is in any state other than `:unscheduled`.
251
+ #
252
+ # @return [Promise] a reference to `self`
231
253
def execute
232
254
if root?
233
255
if compare_and_set_state ( :pending , :unscheduled )
@@ -240,6 +262,9 @@ def execute
240
262
self
241
263
end
242
264
265
+ # @!macro ivar_set_method
266
+ #
267
+ # @raise [Concurrent::PromiseExecutionError] if not the root promise
243
268
def set ( value = IVar ::NO_VALUE , &block )
244
269
raise PromiseExecutionError . new ( 'supported only on root promise' ) unless root?
245
270
check_for_block_or_value! ( block_given? , value )
@@ -253,6 +278,9 @@ def set(value = IVar::NO_VALUE, &block)
253
278
execute
254
279
end
255
280
281
+ # @!macro ivar_fail_method
282
+ #
283
+ # @raise [Concurrent::PromiseExecutionError] if not the root promise
256
284
def fail ( reason = StandardError . new )
257
285
set { raise reason }
258
286
end
@@ -275,6 +303,13 @@ def self.execute(opts = {}, &block)
275
303
new ( opts , &block ) . execute
276
304
end
277
305
306
+ # Chain a new promise off the current promise.
307
+ #
308
+ # @param [Proc] rescuer An optional rescue block to be executed if the
309
+ # promise is rejected.
310
+ #
311
+ # @yield The block operation to be performed asynchronously.
312
+ #
278
313
# @return [Promise] the new promise
279
314
def then ( rescuer = nil , &block )
280
315
raise ArgumentError . new ( 'rescuers and block are both missing' ) if rescuer . nil? && !block_given?
@@ -296,13 +331,23 @@ def then(rescuer = nil, &block)
296
331
child
297
332
end
298
333
299
- # @return [Promise]
334
+ # Chain onto this promise an action to be undertaken on success
335
+ # (fulfillment).
336
+ #
337
+ # @yield The block to execute
338
+ #
339
+ # @return [Promise] self
300
340
def on_success ( &block )
301
341
raise ArgumentError . new ( 'no block given' ) unless block_given?
302
342
self . then ( &block )
303
343
end
304
344
305
- # @return [Promise]
345
+ # Chain onto this promise an action to be undertaken on failure
346
+ # (rejection).
347
+ #
348
+ # @yield The block to execute
349
+ #
350
+ # @return [Promise] self
306
351
def rescue ( &block )
307
352
self . then ( block )
308
353
end
0 commit comments