Module: Libuv::Q
- Included in:
- Deferred
- Defined in:
- lib/libuv/q.rb
Defined Under Namespace
Classes: Deferred, DeferredPromise, Promise, ResolvedPromise
Class Method Summary collapse
-
.all(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
-
.any(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when any of the input promises are resolved.
-
.defer(loop) ⇒ Deferred
Creates a Deferred object which represents a task which will finish in the future.
-
.finally(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved or rejected.
-
.reject(loop, reason = nil) ⇒ Promise
Creates a promise that is resolved as rejected with the specified reason.
Class Method Details
.all(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/libuv/q.rb', line 350 def all(loop, *promises) deferred = Q.defer(loop) counter = promises.length results = [] if counter > 0 promises.each_index do |index| ref(loop, promises[index]).then(proc {|result| if results[index].nil? results[index] = result counter -= 1 deferred.resolve(results) if counter <= 0 end result }, proc {|reason| if results[index].nil? deferred.reject(reason) end Q.reject(@loop, reason) # Don't modify result }) end else deferred.resolve(results) end return deferred.promise end |
.any(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when any of the input promises are resolved.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/libuv/q.rb', line 385 def any(loop, *promises) deferred = Q.defer(loop) if promises.length > 0 promises.each_index do |index| ref(loop, promises[index]).then(proc {|result| deferred.resolve(true) result }, proc {|reason| deferred.reject(false) Q.reject(@loop, reason) # Don't modify result }) end else deferred.resolve(true) end end |
.defer(loop) ⇒ Deferred
Creates a Deferred object which represents a task which will finish in the future.
299 300 301 |
# File 'lib/libuv/q.rb', line 299 def defer(loop) return Deferred.new(loop) end |
.finally(loop, *promises) ⇒ Promise
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved or rejected.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/libuv/q.rb', line 410 def self.finally(loop, *promises) deferred = Q.defer(loop) counter = promises.length results = [] if counter > 0 promises.each_index do |index| ref(loop, promises[index]).then(proc {|result| if results[index].nil? results[index] = [result, false] counter -= 1 deferred.resolve(results) if counter <= 0 end result }, proc {|reason| if results[index].nil? results[index] = [reason, false] counter -= 1 deferred.resolve(results) if counter <= 0 end Q.reject(@loop, reason) # Don't modify result }) end else deferred.resolve(results) end return deferred.promise end |
.reject(loop, reason = nil) ⇒ Promise
Creates a promise that is resolved as rejected with the specified reason. This api should be used to forward rejection in a chain of promises. If you are dealing with the last promise in a promise chain, you don’t need to worry about it.
When comparing deferreds/promises to the familiar behaviour of try/catch/throw, think of reject as the raise keyword in Ruby. This also means that if you “catch” an error via a promise error callback and you want to forward the error to the promise derived from the current promise, you have to “rethrow” the error by returning a rejection constructed via reject.
337 338 339 |
# File 'lib/libuv/q.rb', line 337 def reject(loop, reason = nil) return ResolvedPromise.new(loop, reason, true) # A resolved failed promise end |