 
     promise
 
+  # Creates a new Promise.
+  #
+  # factory - The {Function} to call to resolve the created promise.
   constructor: (@factory) ->
     @pending = true
     @started = false

class agt.Promise
  @unit: (value=0) ->
    promise = new agt.Promise
    promise.resolve value
    promise

  @all: (promises) ->
    promise = new agt.Promise
    solved = 0
    results = []

    promises.forEach (p) ->
      p
      .then (value) ->
        solved++
        results[promises.indexOf p] = value
        promise.resolve results if solved is promises.length

      .fail (reason) ->


    @started = true

  # Setup a hook to reject the promise automatically after a given duration.
  #
  # timeout - The {Number} of milliseconds before the promise is rejected.
  # message - The message {String} to use for the rejection.
  rejectAfter: (@timeout, @message) ->

  ### Internal ###

  # Notifies the different callbacks registered on this promise according
  # to the promise state.
  notifyHandlers: ->
    if @fulfilled
      handler @value for handler in @fulfilledHandlers
    else
      handler @reason for handler in @errorHandlers

  # Returns the signature of the passed-in function.
  #
  # func - The {Function} for which return the signature.
  #
  # Returns an {Array} of the function arguments.
  signature: (func) ->
    re = ///
      ^function
