Class: Promise::When
Instance Method Summary collapse
- #>> ⇒ Object
- #collect(&block) ⇒ Object (also: #map)
- #each(&block) ⇒ Object
-
#initialize(promises = []) ⇒ When
constructor
A new instance of When.
- #inject(*args, &block) ⇒ Object (also: #reduce)
- #try ⇒ Object
- #wait(promise) ⇒ Object (also: #and)
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When.
275 276 277 278 279 280 281 282 283 |
# File 'lib/hyper-operation/promise.rb', line 275 def initialize(promises = []) super() @wait = [] promises.each {|promise| wait promise } end |
Instance Method Details
#>> ⇒ Object
331 332 333 334 335 |
# File 'lib/hyper-operation/promise.rb', line 331 def >>(*) super.tap { try } end |
#collect(&block) ⇒ Object Also known as: map
293 294 295 296 297 298 299 |
# File 'lib/hyper-operation/promise.rb', line 293 def collect(&block) raise ArgumentError, 'no block given' unless block self.then {|values| When.new(values.map(&block)) } end |
#each(&block) ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/hyper-operation/promise.rb', line 285 def each(&block) raise ArgumentError, 'no block given' unless block self.then {|values| values.each(&block) } end |
#inject(*args, &block) ⇒ Object Also known as: reduce
301 302 303 304 305 |
# File 'lib/hyper-operation/promise.rb', line 301 def inject(*args, &block) self.then {|values| values.reduce(*args, &block) } end |
#try ⇒ Object
337 338 339 340 341 342 343 344 345 |
# File 'lib/hyper-operation/promise.rb', line 337 def try if @wait.all?(&:realized?) if promise = @wait.find(&:rejected?) reject(promise.error) else resolve(@wait.map(&:value)) end end end |
#wait(promise) ⇒ Object Also known as: and
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/hyper-operation/promise.rb', line 311 def wait(promise) unless Promise === promise promise = Promise.value(promise) end if promise.act? promise = promise.then end @wait << promise promise.always { try if @next.any? } self end |