Unable to convert return expression of type Promise (_, _) & # 8594; DataRequest to return type Promise <DataResponse, AnyObject >>

Can't convert return expression of type Promise (,) -> DataRequest to return type of Promise>

my function

func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<AnyObject>> {

        var request = URLRequest(url: URL(string: url)!)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")

        request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

        return Promise { fulfill, reject in

        manager.request(request)
            .responseJSON { response in
                fulfill(response)
        }

      

And I am getting this error on the return promise line. How to convert?

I tried changing my backsign to Promise<DataRequest, Error

and getting a compile error on this line that Promise is too specialized with 2 parameters instead of 1.

+1


source to share


1 answer


The problem is fulfill

, because it expects a parameter DataResponse<AnyObject>

, but you are passing DataResponse<Any>

.

Changing the return type of your method postJson

to Promise<DataResponse<Any>>

should solve your problem.

Change this line



func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<AnyObject>> {

      

to

func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<Any>> {

      

+1


source







All Articles