Sha256: 7c0970602b8bfb25e32db386bad09586d294fe9920453a18753e3dcdb897d86d

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'whenner/version'
require 'forwardable'

require 'whenner/conversions'
require 'whenner/deferred_proxy'
require 'whenner/callback'
require 'whenner/deferred'
require 'whenner/promise'

module Whenner
  # Generic root exception for the Whenner library. Any other custom
  # exceptions inherit from WhennerError.
  class WhennerError < StandardError; end

  # Custom exception raised when trying to access a deferred's value or
  # reason before it is resolved.
  #
  # @see WhennerError
  class UnresolvedError < WhennerError; end

  # Custom exception raised when trying to transition an already resolved
  # deferred.
  #
  # @see WhennerError
  class CannotTransitionError < WhennerError; end

  module_function

  # Create a new deferred, resolve it in the block and get its promise back.
  #
  # @yieldparam [Deferred] deferred
  # @return [Promise]
  def defer
    deferred = Deferred.new
    yield deferred
    deferred.promise
  end

  # Create a new deferred based that will resolve if/when the given promises
  # are resolved. Use to combine multiple promises into a single deferred
  # object.
  #
  # When all the given promises are fulfilled, the resulting promise from
  # `when` if fulfilled with an array of all the values. When one of the given
  # promises is rejected, the resulting promise is rejected with that reason.
  #
  # @param [Object] promises
  # @return [Promise]
  def when(*promises)
    defer do |d|
      promises.each_with_object([]) do |promise, values|
        Conversions.Promise(promise).tap do |p|
          p.done  do |value|
            values << value
            d.fulfill(values) if values.size == promises.size
          end
          p.fail do |reason|
            d.reject(reason)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whenner-0.2.0 lib/whenner.rb