 }
 
 
+extension AnyPromise {
+    private func pipe(resolve: (Resolution<AnyObject?>) -> Void) -> Void {
+        pipe { (obj: AnyObject?) in
+            if let error = obj as? NSError {
+                resolve(.Rejected(error, ErrorConsumptionToken(error)))
+            } else {
+                // possibly the value of this promise is a PMKManifold, if so
+                // calling the objc `value` method will return the first item.
+                resolve(.Fulfilled(self.valueForKey("value")))
+            }
+        }
+    }
+}
+
+
 extension Promise {
     @available(*, unavailable, message="T cannot conform to ErrorType")
     public convenience init<T: ErrorType>(@noescape resolvers: (fulfill: (T) -> Void, reject: (ErrorType) -> Void) throws -> Void) { abort() }

                resolve(.Rejected(error))
            case .Fulfilled(let value):
                contain_zalgo(q, rejecter: resolve) {
                    let anypromise = try body(value)
                    anypromise.pipe { obj in
                        if let error = obj as? NSError {
                            resolve(.Rejected(error, ErrorConsumptionToken(error as ErrorType)))
                        } else {
                            // possibly the value of this promise is a PMKManifold, if so
                            // calling the objc `value` method will return the first item.
                            let obj: AnyObject? = anypromise.valueForKey("value")
                            resolve(.Fulfilled(obj))
                        }
                    }
                }
            }
        }

    }
}

/**
 `firstly` can make chains more readable.

 Compare:

     SCNetworkReachability().then {
         NSURLSession.GET(url2)
     }.then {
         NSURLSession.GET(url3)
     }

 With:

     firstly {
         SCNetworkReachability()
     }.then {
         NSURLSession.GET(url2)
     }.then {
         NSURLSession.GET(url3)
     }
*/
public func firstly(@noescape promise: () throws -> AnyPromise) -> Promise<AnyObject?> {
    return Promise { resolve in
        try promise().pipe(resolve)
    }
}

@available(*, unavailable, message="Instead, throw")
public func firstly<T: ErrorType>(@noescape promise: () throws -> Promise<T>) -> Promise<T> {
