Module: Q

Defined in:
lib/q.rb,
lib/q/version.rb

Defined Under Namespace

Classes: Callback, Defer, Promise

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.defer(&block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/q.rb', line 7

def self.defer(&block)
  defer = Q::Defer.new

  block.call(defer)

  defer.promise
end

.when(promises) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/q.rb', line 15

def self.when(promises)
  defer = Q::Defer.new

  @results = []

  promises.each do |promise|
    promise.then do |result|
      @results << result
      defer.resolve(@results) if @results.length == promises.length
    end.fail do |result|
      defer.reject([result, @results])
    end
  end

  defer.promise
end