Search code examples
swiftalamofirepromisekit

PromiseKit 6 error in cannot convert error


Firstly, I'm aware that the implementation has changed in v6 and and I using the seal object as intended, the problem I'm having is that even when following the example to the letter it still gives me the old Cannot convert value of type '(_) -> CustomerLoginResponse' to expected argument type '(_) -> _' error.

Here is my function that returns the promise:

 static func makeCustomerLoginRequest(userName: String, password: String) -> Promise<CustomerLoginResponse>
{
    return Promise
        { seal in
            Alamofire.request(ApiProvider.buildUrl(), method: .post, parameters: ApiObjectFactory.Requests.createCustomerLoginRequest(userName: userName, password: password).toXML(), encoding: XMLEncoding.default, headers: Constants.Header)
                     .responseXMLObject { (resp: DataResponse<CustomerLoginResponse>) in
                if let error =  resp.error
                {
                    seal.reject(error)
                }
                guard let Xml = resp.result.value else {
                    return seal.reject(ApiError.credentialError)
                }
                seal.fulfill(Xml)
            }
    }
}

and here is the function that is consuming it:

static func Login(userName: String, password: String) {
    ApiClient.makeCustomerLoginRequest(userName: userName, password: password).then { data -> CustomerLoginResponse  in

    }
}

Solution

  • You might have to provide more information if you want to chain more than one promises. In v6, you need to use .done if you don't want to continue the promises chain. If you have only one promise with this request then below is the correct implementation.

    static func Login(userName: String, password: String) {
        ApiClient.makeCustomerLoginRequest(userName: userName, password: password)
             .done { loginResponse in
                  print(loginResponse)
             }.catch { error in
                  print(error)
             }
    }
    

    Remember, you have to return a promise if you are using .then until you break the chain by using .done. If you want to chain multiple promises then your syntax should look like this,

    ApiClient.makeCustomerLoginRequest(userName: userName, password: password)
           .then { loginResponse -> Promise<CustomerLoginResponse> in
                 return .value(loginResponse)
            }.then { loginResponse -> Promise<Bool> in
                 print(loginResponse)
                 return .value(true)
            }.then { bool -> Promise<String> in
                 print(bool)
                 return .value("hello world")
            }.then { string -> Promise<Int> in
                 print(string)
                 return .value(100)
            }.done { int in
                 print(int)
            }.catch { error in
                 print(error)
            }