Skip to content

Commit 99f5f0a

Browse files
committed
changed default Promise#then behaviour without rescue callable
1 parent 213adea commit 99f5f0a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/concurrent/promise.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ class Promise
1212
# @see http://wiki.commonjs.org/wiki/Promises/A
1313
# @see http://promises-aplus.github.io/promises-spec/
1414
def initialize(options = {}, &block)
15+
options.delete_if {|k, v| v.nil?}
16+
1517
@parent = options.fetch(:parent) { nil }
1618
@on_fulfill = options.fetch(:on_fulfill) { Proc.new{ |result| result } }
17-
@on_reject = options.fetch(:on_reject) { Proc.new{ |result| result } }
19+
@on_reject = options.fetch(:on_reject) { Proc.new{ |reason| raise reason } }
1820

1921
@promise_body = block || Proc.new{|result| result }
2022
@state = :unscheduled

spec/concurrent/promise_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ module Concurrent
307307
expected.should eq 20
308308
end
309309

310+
it 'uses result as fulfillment value when a promise has no block' do
311+
p = Promise.new{ 20 }.then(Proc.new{}).execute
312+
sleep(0.1)
313+
p.value.should eq 20
314+
end
315+
310316
it 'can manage long chain' do
311317
root = Promise.new { 20 }
312318
p1 = root.then { |b| b * 3 }
@@ -345,6 +351,13 @@ module Concurrent
345351
p.should be_rejected
346352
end
347353

354+
it 'uses reason as rejection reason when a promise has no rescue callable' do
355+
p = Promise.new{ raise ArgumentError }.then { |val| val }.execute
356+
sleep(0.1)
357+
p.should be_rejected
358+
p.reason.should be_a ArgumentError
359+
end
360+
348361
end
349362

350363
context 'aliases' do

0 commit comments

Comments
 (0)