Skip to content

Commit 0ee31d0

Browse files
authored
Merge pull request #729 from reitermarkus/promise-then
Fix `Promise#then`.
2 parents 9d2505c + 244ad6a commit 0ee31d0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/concurrent/promise.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,16 @@ def self.execute(opts = {}, &block)
307307
# @yield The block operation to be performed asynchronously.
308308
#
309309
# @return [Promise] the new promise
310-
def then(rescuer = nil, executor = @executor, &block)
310+
def then(*args, &block)
311+
if args.last.is_a?(::Hash)
312+
executor = args.pop[:executor]
313+
rescuer = args.first
314+
else
315+
rescuer, executor = args
316+
end
317+
318+
executor ||= @executor
319+
311320
raise ArgumentError.new('rescuers and block are both missing') if rescuer.nil? && !block_given?
312321
block = Proc.new { |result| result } unless block_given?
313322
child = Promise.new(

spec/concurrent/promise_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ def get_ivar_from_args(opts)
225225
expect(child).not_to be empty_root
226226
expect(child.instance_variable_get(:@executor)).to be(new_executor)
227227
end
228+
229+
it 'supports setting the executor using a named parameter' do
230+
new_executor = Concurrent::SingleThreadExecutor.new
231+
child = empty_root.then(executor: new_executor) { nil }
232+
expect(child.instance_variable_get(:@executor)).to be(new_executor)
233+
end
234+
228235
it 'should have block or rescuers' do
229236
expect { empty_root.then }.to raise_error(ArgumentError)
230237
end

0 commit comments

Comments
 (0)