Module: When

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

Overview

– The MIT License (MIT)

Copyright © 2012 Gitorious AS

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Defined Under Namespace

Classes: AlreadyResolvedError, Deferred, Promise, Resolver

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.all(promises) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/when.rb', line 94

def self.all(promises)
  raise(ArgumentError, "expected enumerable promises") if !promises.is_a?(Enumerable)
  resolved = 0
  results = []
  d = Deferred.new

  attempt_resolution = lambda do |err, res|
    break if d.resolved?
    if err.nil?
      d.resolve(res) if promises.length == resolved
    else
      d.reject(err)
    end
  end

  wait_for_all(promises) do |err, result, index|
    resolved += 1
    results[index] = result
    attempt_resolution.call(err, results)
  end

  attempt_resolution.call(nil, results) if promises.length == 0
  d.promise
end

.defer {|deferred| ... } ⇒ Object

Yields:

  • (deferred)


76
77
78
79
80
# File 'lib/when.rb', line 76

def self.defer
  deferred = Deferred.new
  yield deferred if block_given?
  deferred
end

.reject(val) ⇒ Object



88
89
90
91
92
# File 'lib/when.rb', line 88

def self.reject(val)
  deferred = Deferred.new
  deferred.reject(val)
  deferred.promise
end

.resolve(val) ⇒ Object



82
83
84
85
86
# File 'lib/when.rb', line 82

def self.resolve(val)
  deferred = Deferred.new
  deferred.resolve(val)
  deferred.promise
end