Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/concurrent/promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,16 @@ def self.execute(opts = {}, &block)
# @yield The block operation to be performed asynchronously.
#
# @return [Promise] the new promise
def then(rescuer = nil, executor = @executor, &block)
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(
Expand Down
7 changes: 7 additions & 0 deletions spec/concurrent/promise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ def get_ivar_from_args(opts)
expect(child).not_to be empty_root
expect(child.instance_variable_get(:@executor)).to be(new_executor)
end

it 'supports setting the executor using a named parameter' do
new_executor = Concurrent::SingleThreadExecutor.new
child = empty_root.then(executor: new_executor) { nil }
expect(child.instance_variable_get(:@executor)).to be(new_executor)
end

it 'should have block or rescuers' do
expect { empty_root.then }.to raise_error(ArgumentError)
end
Expand Down