Skip to content

Commit

Permalink
Merge pull request #729 from reitermarkus/promise-then
Browse files Browse the repository at this point in the history
Fix `Promise#then`.
  • Loading branch information
pitr-ch committed Jun 19, 2018
2 parents 9d2505c + 244ad6a commit 0ee31d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 0ee31d0

Please sign in to comment.