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! , #to_v2 , #trace , #trace! , #value , value , when
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When.
384 385 386 387 388 389 390 391 392
# File 'opal/stdlib/promise.rb', line 384 def initialize(promises = []) super() @wait = [] promises.each do |promise| wait promise end end
Instance Method Details
#>> ⇒ Object
434 435 436 437 438
# File 'opal/stdlib/promise.rb', line 434 def >>(*) super.tap do try end end
#each(&block) ⇒ Object
Raises:
- (ArgumentError)
394 395 396 397 398 399 400
# File 'opal/stdlib/promise.rb', line 394 def each(&block) raise ArgumentError, 'no block given' unless block self.then do |values| values.each(&block) end end
#inject(*args, &block) ⇒ Object Also known as: reduce
410 411 412 413 414
# File 'opal/stdlib/promise.rb', line 410 def inject(*args, &block) self.then do |values| values.reduce(*args, &block) end end
#try ⇒ Object
440 441 442 443 444 445 446 447 448 449
# File 'opal/stdlib/promise.rb', line 440 def try if @wait.all?(&:realized?) promise = @wait.find(&:rejected?) if promise reject(promise.error) else resolve(@wait.map(&:value)) end end end
#wait(promise) ⇒ Object Also known as: and
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
# File 'opal/stdlib/promise.rb', line 416 def wait(promise) unless Promise === promise promise = Promise .value (promise) end if promise.act? promise = promise.then end @wait << promise promise.always do try if @next.any? end self end