Skip to content

Commit 5541da2

Browse files
committed
Handle failures in the block passed to Promise#flat_map
1 parent 3111149 commit 5541da2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/concurrent/promise.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,14 @@ def flat_map(&block)
126126

127127
on_error { |e| child.on_reject(e) }
128128
on_success do |result1|
129-
inner = block.call(result1)
130-
inner.execute
131-
inner.on_success { |result2| child.on_fulfill(result2) }
132-
inner.on_error { |e| child.on_reject(e) }
129+
begin
130+
inner = block.call(result1)
131+
inner.execute
132+
inner.on_success { |result2| child.on_fulfill(result2) }
133+
inner.on_error { |e| child.on_reject(e) }
134+
rescue => e
135+
child.on_reject(e)
136+
end
133137
end
134138

135139
child

spec/concurrent/promise_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ module Concurrent
289289
expect(child).to be_rejected
290290
end
291291

292+
it 'fails if the generating block fails' do
293+
child = Promise.new(executor: executor) { }.flat_map { fail }.execute.wait
294+
295+
expect(child).to be_rejected
296+
end
297+
292298
end
293299

294300
describe '#zip' do

0 commit comments

Comments
 (0)