@@ -30,14 +30,14 @@ class Promise
3030 # @see http://wiki.commonjs.org/wiki/Promises/A
3131 # @see http://promises-aplus.github.io/promises-spec/
3232 def initialize ( opts = { } , &block )
33- opts . delete_if { |k , v | v . nil? }
33+ opts . delete_if { |k , v | v . nil? }
3434
3535 @executor = OptionsParser ::get_executor_from ( opts )
3636 @parent = opts . fetch ( :parent ) { nil }
37- @on_fulfill = opts . fetch ( :on_fulfill ) { Proc . new { |result | result } }
38- @on_reject = opts . fetch ( :on_reject ) { Proc . new { |reason | raise reason } }
37+ @on_fulfill = opts . fetch ( :on_fulfill ) { Proc . new { |result | result } }
38+ @on_reject = opts . fetch ( :on_reject ) { Proc . new { |reason | raise reason } }
3939
40- @promise_body = block || Proc . new { |result | result }
40+ @promise_body = block || Proc . new { |result | result }
4141 @state = :unscheduled
4242 @children = [ ]
4343
@@ -46,13 +46,13 @@ def initialize(opts = {}, &block)
4646
4747 # @return [Promise]
4848 def self . fulfill ( value , opts = { } )
49- Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , true , value , nil ) }
49+ Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , true , value , nil ) }
5050 end
5151
5252
5353 # @return [Promise]
5454 def self . reject ( reason , opts = { } )
55- Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , false , nil , reason ) }
55+ Promise . new ( opts ) . tap { |p | p . send ( :synchronized_set_state! , false , nil , reason ) }
5656 end
5757
5858 # @return [Promise]
@@ -77,7 +77,7 @@ def self.execute(opts = {}, &block)
7777 # @return [Promise] the new promise
7878 def then ( rescuer = nil , &block )
7979 raise ArgumentError . new ( 'rescuers and block are both missing' ) if rescuer . nil? && !block_given?
80- block = Proc . new { |result | result } if block . nil?
80+ block = Proc . new { |result | result } if block . nil?
8181 child = Promise . new (
8282 parent : self ,
8383 executor : @executor ,
@@ -105,6 +105,7 @@ def on_success(&block)
105105 def rescue ( &block )
106106 self . then ( block )
107107 end
108+
108109 alias_method :catch , :rescue
109110 alias_method :on_error , :rescue
110111
@@ -124,13 +125,13 @@ def root? # :nodoc:
124125
125126 # @!visibility private
126127 def on_fulfill ( result )
127- realize Proc . new { @on_fulfill . call ( result ) }
128+ realize Proc . new { @on_fulfill . call ( result ) }
128129 nil
129130 end
130131
131132 # @!visibility private
132133 def on_reject ( reason )
133- realize Proc . new { @on_reject . call ( reason ) }
134+ realize Proc . new { @on_reject . call ( reason ) }
134135 nil
135136 end
136137
@@ -142,14 +143,14 @@ def notify_child(child)
142143 # @!visibility private
143144 def realize ( task )
144145 @executor . post do
145- success , value , reason = SafeTaskExecutor . new ( task ) . execute
146+ success , value , reason = SafeTaskExecutor . new ( task ) . execute
146147
147148 children_to_notify = mutex . synchronize do
148149 set_state! ( success , value , reason )
149150 @children . dup
150151 end
151152
152- children_to_notify . each { |child | notify_child ( child ) }
153+ children_to_notify . each { |child | notify_child ( child ) }
153154 end
154155 end
155156
@@ -159,9 +160,9 @@ def set_state!(success, value, reason)
159160 end
160161
161162 def synchronized_set_state! ( success , value , reason )
162- mutex . synchronize do
163- set_state! ( success , value , reason )
164- end
163+ mutex . lock
164+ set_state! ( success , value , reason )
165+ mutex . unlock
165166 end
166167 end
167168end
0 commit comments