Skip to content

Commit 6c0bbb4

Browse files
authored
Merge pull request #572 from lucasallan/promises-then-with-executor
Allows Promise#then to receive an executor
2 parents 3e7328a + 51780b6 commit 6c0bbb4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/concurrent/promise.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module Concurrent
7979
# ```
8080
#
8181
# Promises can be chained using the `then` method. The `then` method accepts a
82-
# block, to be executed on fulfillment, and a callable argument to be executed
82+
# block and an executor, to be executed on fulfillment, and a callable argument to be executed
8383
# on rejection. The result of the each promise is passed as the block argument
8484
# to chained promises.
8585
#
@@ -93,7 +93,7 @@ module Concurrent
9393
# p = Concurrent::Promise.fulfill(20).
9494
# then{|result| result - 10 }.
9595
# then{|result| result * 3 }.
96-
# then{|result| result % 5 }.execute
96+
# then(executor: different_executor){|result| result % 5 }.execute
9797
# ```
9898
#
9999
# The initial state of a newly created Promise depends on the state of its parent:
@@ -301,15 +301,18 @@ def self.execute(opts = {}, &block)
301301
# @param [Proc] rescuer An optional rescue block to be executed if the
302302
# promise is rejected.
303303
#
304+
# @param [ThreadPool] executor An optional thread pool executor to be used
305+
# in the new Promise
306+
#
304307
# @yield The block operation to be performed asynchronously.
305308
#
306309
# @return [Promise] the new promise
307-
def then(rescuer = nil, &block)
310+
def then(rescuer = nil, executor = @executor, &block)
308311
raise ArgumentError.new('rescuers and block are both missing') if rescuer.nil? && !block_given?
309312
block = Proc.new { |result| result } unless block_given?
310313
child = Promise.new(
311314
parent: self,
312-
executor: @executor,
315+
executor: executor,
313316
on_fulfill: block,
314317
on_reject: rescuer
315318
)

spec/concurrent/promise_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ def get_ivar_from_args(opts)
218218
expect(child).not_to be empty_root
219219
end
220220

221+
it 'returns a new promise when a block, rescuer and executor are passed' do
222+
new_executor = Concurrent::SingleThreadExecutor.new
223+
child = empty_root.then(Proc.new{}, new_executor) { nil }
224+
expect(child).to be_a Promise
225+
expect(child).not_to be empty_root
226+
expect(child.instance_variable_get(:@executor)).to be(new_executor)
227+
end
221228
it 'should have block or rescuers' do
222229
expect { empty_root.then }.to raise_error(ArgumentError)
223230
end

0 commit comments

Comments
 (0)