Promisekit: Promise Kit returns with multiple values

Created on 9 Apr 2018  Â·  2Comments  Â·  Source: mxcl/PromiseKit

This issue is occurring with PromiseKit 6.2.4

I'm currently migrating some older code from PromiseKit 4.x to the latest release. Overall the new features especially done are fantastic. I'm having a bit of trouble converting the following statements from PromiseKit 4.x to the 6.2.4.

return Promise <Value> { seal in

self.performFirstNetworkingOperation().then { firstResponse -> Promise<(NSDictionary, NSDictionary)> in
        return self.secondNetworkingOperation().map { (firstResponse, $0)}   // If an error occurs here it can be lost 
        /* This caused the enclosing promise to reject in PromiseKit 4.x 
        return self.secondNetworkingOperation().map { (firstResponse, $0)}.catch{seal.reject($0)}
                Swift now complains that "Cannot convert return expression of type 'PMKFinalizer' to return type 'Promise<(NSDictionary, NSDictionary)>' "

                */
}.done { result in
        let (firstResponse, secondResponse) = result
        …
                seal.fulfill(value)
}.catch  { error in
        …
        seal.reject( error)
}

}

I essentially want to return multiple values and handle an error if it occurs. Is this possible with the latest changes to PromiseKit?

Thank you

Most helpful comment

This is all you need:

return firstly {
    performFirstNetworkingOperation()
}.then { firstResponse in
    self.secondNetworkingOperation().map { (firstResponse, $0) }
}

All 2 comments

This is all you need:

return firstly {
    performFirstNetworkingOperation()
}.then { firstResponse in
    self.secondNetworkingOperation().map { (firstResponse, $0) }
}

Wrapping promises in promises makes things harder than necessary. I suggest reading our common mis-usage guide in the Documentation appendix.

Was this page helpful?
0 / 5 - 0 ratings