             self.onFulfilled(value)
         }
     }
-    
-    // MARK: - Type
-
-    public typealias APlusResovler = (value: Any?) -> Void
-    public typealias APlusRejector = (reason: Any?) -> Void
-
-    enum State {
-        case Pending, Fulfilled, Rejected
+
+    // MARK: - Public APIs
+
+    public class func resolve(value: Any?) -> APlusPromise
+    {
+        switch value {
+        case let promise as APlusPromise:
+            return promise
+        default:
+            return self(value: value)
+        }
+    }
+    
+    public class func reject(reason: Any?) -> Self
+    {
+        return self(reason: reason)
+    }
+    
+    public class func all(values: [Any?]) -> Self
+    {
+        let allPromise = self()
+        let count = values.count;
+        var results: [Any?] = []
+
+        for value in values
+        {
+            let promise = self.resolve(value)
+            promise.then(
+                onFulfilled: { (value) -> Any? in
+                    if .Pending == allPromise.state {
+                        results.append(value)
+                        if results.count >= count {
+                            allPromise.onFulfilled(results)
+                        }
+                    }
+                    return nil
+                },
+                onRejected: { (reason) -> Any? in
+                    if .Pending == allPromise.state {
+                        allPromise.onRejected(reason)
+                    }
+                    return nil
+                }
+            )
+        }
+        
+        return allPromise
+    }
+    
+    public class func race(values: [Any?]) -> Self
+    {
+        let racePromise = self()
+        
+        for value in values
+        {
+            let promise = self.resolve(value)
+            promise.then(
+                onFulfilled: { (value) -> Any? in
+                    if .Pending == racePromise.state {
+                        racePromise.onFulfilled(value)
+                    }
+                    return nil
+                },
+                onRejected: { (reason) -> Any? in
+                    if .Pending == racePromise.state {
+                        racePromise.onRejected(reason)
+                    }
+                    return nil
+                }
+            )
+        }
+
+        return self(value: nil)
+    }
+    
+    public func catch(onRejected: Rejection) -> Thenable
+    {
+        return self.then(
+            onFulfilled: nil,
+            onRejected: onRejected
+        );
+    }
+
+    // MARK: - Thenable
+    
+    public func then(onFulfilled: Resolution? = nil, onRejected: Rejection? = nil) -> Thenable
+    {
+        var subPromise: APlusPromise
+        
+        switch self.state {
+        case .Pending:
+            subPromise = self.dynamicType()
+        case .Fulfilled:
+            subPromise = self.dynamicType(value: self.value)
+        case .Rejected:
+            subPromise = self.dynamicType(reason: self.reason)
+        }
+        
+        let then: (resolution: Resolution?, rejection: Rejection?, subPromise: APlusPromise) = (onFulfilled, onRejected, subPromise)
+        self.thens.append(then)
+        
+        return subPromise
     }
 }
        )
    }
    
    
    // MARK: - Public APIs

    public class func resolve(value: Any?) -> APlusPromise
    {
        switch value {
        case let promise as APlusPromise:
            return promise
        default:
            return self(value: value)
        }
    }
    
    public class func reject(reason: Any?) -> Self
    {
        return self(reason: reason)
    }
    
    public class func all(values: [Any?]) -> Self
    {
        let allPromise = self()
        let count = values.count;
        var results: [Any?] = []

        for value in values
        {
            let promise = self.resolve(value)
            promise.then(
                onFulfilled: { (value) -> Any? in
                    if .Pending == allPromise.state {
                        results.append(value)
                        if results.count >= count {
                            allPromise.onFulfilled(results)
                        }
                    }
                    return nil
                },
                onRejected: { (reason) -> Any? in
                    if .Pending == allPromise.state {
                        allPromise.onRejected(reason)
                    }
                    return nil
                }
            )
        }
        
        return allPromise
    }
    
    public class func race(values: [Any?]) -> Self
    {
        let racePromise = self()
        
        for value in values
        {
            let promise = self.resolve(value)
            promise.then(
                onFulfilled: { (value) -> Any? in
                    if .Pending == racePromise.state {
                        racePromise.onFulfilled(value)
                    }
                    return nil
                },
                onRejected: { (reason) -> Any? in
                    if .Pending == racePromise.state {
                        racePromise.onRejected(reason)
                    }
                    return nil
                }
            )
        }

        return self(value: nil)
    }
    
    // MARK: - Thenable
    
    public func then(onFulfilled: Resolution? = nil, onRejected: Rejection? = nil) -> Thenable
    {
        var subPromise: APlusPromise
        
        switch self.state {
        case .Pending:
            subPromise = self.dynamicType()
        case .Fulfilled:
            subPromise = self.dynamicType(value: self.value)
        case .Rejected:
            subPromise = self.dynamicType(reason: self.reason)
        }
        
        let then: (resolution: Resolution?, rejection: Rejection?, subPromise: APlusPromise) = (onFulfilled, onRejected, subPromise)
        self.thens.append(then)
        
        return subPromise
    }
    
    public func catch(onRejected: Rejection) -> Thenable
    {
        return self.then(
            onFulfilled: nil,
            onRejected: onRejected
        );
    }
    
    // MARK: - Private APIs
    
    func onFulfilled(value: Any?) -> Void


public class APlusPromise: Thenable
{
    // MARK: - Type
    
    public typealias APlusResovler = (value: Any?) -> Void
    public typealias APlusRejector = (reason: Any?) -> Void
    
    enum State {
        case Pending, Fulfilled, Rejected
    }

    // MAKR: ivars
    
    var state: State
    var value: Any?
    var reason: Any?
