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 , error , #exception! , #exception? , #fail , #inspect , #realized? , #reject , #reject! , #rejected? , #resolve , #resolve! , #resolved? , #then , #trace , value , #value , when
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When
355 356 357 358 359 360 361 362 363
# File 'opal/stdlib/promise.rb', line 355 def initialize(promises = []) super() @wait = [] promises.each {|promise| wait promise } end
Instance Method Details
#>> ⇒ Object
411 412 413 414 415
# File 'opal/stdlib/promise.rb', line 411 def >>(*) super.tap { try } end
#each(&block) ⇒ Object
Raises:
- (ArgumentError)
365 366 367 368 369 370 371
# File 'opal/stdlib/promise.rb', line 365 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
381 382 383 384 385
# File 'opal/stdlib/promise.rb', line 381 def inject(*args, &block) self.then {|values| values.reduce(*args, &block) } end
#try ⇒ Object
417 418 419 420 421 422 423 424 425
# File 'opal/stdlib/promise.rb', line 417 def try if @wait.all?(&:realized?) if promise = @wait.find(&:rejected?) reject(promise.error) else resolve(@wait.map(&:value)) end end end