Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Promise#then. #729

Merged
merged 1 commit into from Jun 19, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/concurrent/promise.rb
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
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