Class: Promise::When
Instance Attribute Summary
Attributes inherited from Promise
#error, #next, #prev, #value
Instance Method Summary
collapse
Methods inherited from Promise
#<<, #^, #act?, #always, error, #exception!, #exception?, #fail, #inspect, #realized?, #reject, #reject!, #rejected?, #resolve, #resolve!, #resolved?, #then, #trace, value, when
Constructor Details
#initialize(promises = []) ⇒ When
Returns a new instance of When
220
221
222
223
224
225
226
227
228
|
# File 'opal/stdlib/promise.rb', line 220
def initialize(promises = [])
super()
@wait = []
promises.each {|promise|
wait promise
}
end
|
Instance Method Details
276
277
278
279
280
|
# File 'opal/stdlib/promise.rb', line 276
def >>(*)
super.tap {
try
}
end
|
#collect(&block) ⇒ Object
Also known as:
map
238
239
240
241
242
243
244
|
# File 'opal/stdlib/promise.rb', line 238
def collect(&block)
raise ArgumentError, 'no block given' unless block
self.then {|values|
When.new(values.map(&block))
}
end
|
#each(&block) ⇒ Object
230
231
232
233
234
235
236
|
# File 'opal/stdlib/promise.rb', line 230
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
246
247
248
249
250
|
# File 'opal/stdlib/promise.rb', line 246
def inject(*args, &block)
self.then {|values|
values.reduce(*args, &block)
}
end
|
282
283
284
285
286
287
288
289
290
|
# File 'opal/stdlib/promise.rb', line 282
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'opal/stdlib/promise.rb', line 256
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
|