                             values.append(value)
 
                             if values.count == promises.count {
-                                promise.fulfill(values)
+                                deferred.fulfill(values)
                                 pending = false
                                 values.removeAll(keepCapacity: false)
                             }

                    dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER)
                    do {
                        if pending {
                            promise.reject(error)
                            pending = false
                            values.removeAll(keepCapacity: false)
                        }

    }

    class func resolve(value: ValueType) -> Promise<ValueType> {
        return Promise<ValueType> { (fulfill, _) -> Void in
            fulfill(value)
        }
    }

    /// Create a promise that is rejected with given error.
    class func reject(error: NSError) -> Promise<ValueType> {
        return Promise<ValueType> { (_, reject) -> Void in
            reject(error)
        }
    }
}

