Class: Promise::When
Instance Attribute Summary
Attributes inherited from Promise
#error, #next, #prev
Instance Method Summary
collapse
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.
385
386
387
388
389
390
391
392
393
|
# File 'opal/stdlib/promise.rb', line 385
def initialize(promises = [])
super()
@wait = []
promises.each do |promise|
wait promise
end
end
|
Instance Method Details
441
442
443
444
445
|
# File 'opal/stdlib/promise.rb', line 441
def >>(*)
super.tap do
try
end
end
|
#collect(&block) ⇒ Object
Also known as:
map
403
404
405
406
407
408
409
|
# File 'opal/stdlib/promise.rb', line 403
def collect(&block)
raise ArgumentError, 'no block given' unless block
self.then do |values|
When.new(values.map(&block))
end
end
|
#each(&block) ⇒ Object
395
396
397
398
399
400
401
|
# File 'opal/stdlib/promise.rb', line 395
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
411
412
413
414
415
|
# File 'opal/stdlib/promise.rb', line 411
def inject(*args, &block)
self.then do |values|
values.reduce(*args, &block)
end
end
|
447
448
449
450
451
452
453
454
455
456
|
# File 'opal/stdlib/promise.rb', line 447
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# File 'opal/stdlib/promise.rb', line 421
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
|