Class: Promise::When
Instance Attribute Summary
Attributes inherited from Promise
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)
Methods inherited from Promise
#<< , #^ , #act? , #action , #always , #always! , error , #exception! , #exception? , #fail , #fail! , #inspect , #realized? , #reject , #reject! , #rejected? , #resolve , #resolve! , #resolved? , #then , #then! , #there_can_be_only_one! , #trace , #trace! , #value , value , when
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When
370 371 372 373 374 375 376 377 378
# File 'opal/stdlib/promise.rb', line 370 def initialize(promises = []) super() @wait = [] promises.each {|promise| wait promise } end
Instance Method Details
#>> ⇒ Object
426 427 428 429 430
# File 'opal/stdlib/promise.rb', line 426 def >>(*) super.tap { try } end
#each(&block) ⇒ Object
Raises:
- (ArgumentError)
380 381 382 383 384 385 386
# File 'opal/stdlib/promise.rb', line 380 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
396 397 398 399 400
# File 'opal/stdlib/promise.rb', line 396 def inject(*args, &block) self.then {|values| values.reduce(*args, &block) } end
#try ⇒ Object
432 433 434 435 436 437 438 439 440
# File 'opal/stdlib/promise.rb', line 432 def try if @wait.all?(&:realized?) if promise = @wait.find(&:rejected?) reject(promise.error) else resolve(@wait.map(&:value)) end end end