Class: Promise::When
Instance Attribute Summary
Attributes inherited from Promise
#error, #next, #prev
Instance Method Summary
collapse
Methods inherited from Promise
#<<, #^, #act?, #action, #always, error, #exception!, #exception?, #fail, #inspect, #method_missing, #realized?, #reject, #reject!, #rejected?, #resolve, #resolve!, #resolved?, #respond_to_missing?, #sync, #then, #to_json, #trace, #value, value, #value_or_error, when
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When.
357
358
359
360
361
362
363
364
365
|
# File 'lib/volt/utils/promise.rb', line 357
def initialize(promises = [])
super()
@wait = []
promises.each {|promise|
wait promise
}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Promise
Instance Method Details
413
414
415
416
417
|
# File 'lib/volt/utils/promise.rb', line 413
def >>(*)
super.tap {
try
}
end
|
#collect(&block) ⇒ Object
Also known as:
map
375
376
377
378
379
380
381
|
# File 'lib/volt/utils/promise.rb', line 375
def collect(&block)
raise ArgumentError, 'no block given' unless block
self.then {|values|
When.new(values.map(&block))
}
end
|
#each(&block) ⇒ Object
367
368
369
370
371
372
373
|
# File 'lib/volt/utils/promise.rb', line 367
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
383
384
385
386
387
|
# File 'lib/volt/utils/promise.rb', line 383
def inject(*args, &block)
self.then {|values|
values.reduce(*args, &block)
}
end
|
419
420
421
422
423
424
425
426
427
|
# File 'lib/volt/utils/promise.rb', line 419
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
# File 'lib/volt/utils/promise.rb', line 393
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
}
self
end
|