Method: Concurrent::Promise#then

Defined in:
lib/concurrent-ruby/concurrent/promise.rb

#then(rescuer, executor, &block) ⇒ Promise #then(rescuer, executor: executor, &block) ⇒ Promise

Chain a new promise off the current promise.

Overloads:

  • #then(rescuer, executor, &block) ⇒ Promise

    Parameters:

    • rescuer (Proc)

      An optional rescue block to be executed if the promise is rejected.

    • executor (ThreadPool)

      An optional thread pool executor to be used in the new Promise

  • #then(rescuer, executor: executor, &block) ⇒ Promise

    Parameters:

    • rescuer (Proc)

      An optional rescue block to be executed if the promise is rejected.

    • executor (ThreadPool) (defaults to: executor)

      An optional thread pool executor to be used in the new Promise

Yields:

  • The block operation to be performed asynchronously.

Returns:

Raises:

  • (ArgumentError)


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/concurrent-ruby/concurrent/promise.rb', line 314

def then(*args, &block)
  if args.last.is_a?(::Hash)
    executor = args.pop[:executor]
    rescuer = args.first
  else
    rescuer, executor = args
  end

  executor ||= @executor

  raise ArgumentError.new('rescuers and block are both missing') if rescuer.nil? && !block_given?
  block = Proc.new { |result| result } unless block_given?
  child = Promise.new(
    parent: self,
    executor: executor,
    on_fulfill: block,
    on_reject: rescuer
  )

  synchronize do
    child.state = :pending if @state == :pending
    child.on_fulfill(apply_deref_options(@value)) if @state == :fulfilled
    child.on_reject(@reason) if @state == :rejected
    @children << child
  end

  child
end