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, #pending?, #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.
275
276
277
278
279
280
281
282
283
|
# File 'lib/hyper-operation/promise.rb', line 275
def initialize(promises = [])
super()
@wait = []
promises.each {|promise|
wait promise
}
end
|
Instance Method Details
#>> ⇒ Object
331
332
333
334
335
|
# File 'lib/hyper-operation/promise.rb', line 331
def >>(*)
super.tap {
try
}
end
|
#collect(&block) ⇒ Object
Also known as:
map
293
294
295
296
297
298
299
|
# File 'lib/hyper-operation/promise.rb', line 293
def collect(&block)
raise ArgumentError, 'no block given' unless block
self.then {|values|
When.new(values.map(&block))
}
end
|
#each(&block) ⇒ Object
285
286
287
288
289
290
291
|
# File 'lib/hyper-operation/promise.rb', line 285
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
301
302
303
304
305
|
# File 'lib/hyper-operation/promise.rb', line 301
def inject(*args, &block)
self.then {|values|
values.reduce(*args, &block)
}
end
|
#try ⇒ Object
337
338
339
340
341
342
343
344
345
|
# File 'lib/hyper-operation/promise.rb', line 337
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/hyper-operation/promise.rb', line 311
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.any?
}
self
end
|