7

I have following code in swift 3.0, Where I am using PromiseKit.

func calculateTravelTime(from: CLLocation, to: CLLocation) -> Promise<Double> {
     Promise<Double> { completion, reject -> Void in
        let request = MKDirections.Request()
        request.transportType = .walking

        let fromPlacemark = MKPlacemark(coordinate: from.coordinate)
        let toPlacemark = MKPlacemark(coordinate: to.coordinate)

        let fromMapPoint = MKMapItem(placemark: fromPlacemark)
        let toMapPoint = MKMapItem(placemark: toPlacemark)

        request.source = fromMapPoint
        request.destination = toMapPoint
        request.requestsAlternateRoutes = false

        let directions = MKDirections(request: request)
        directions.calculate { response, error in
            if error != nil {
                reject(error!)
            } else {
                let time = (response?.routes.first?.expectedTravelTime ?? 0) / 60.0
               completion(time)
            }
        }

    }
} 

It's giving the following error on the second line : "Unable to infer closure type in the current context"

error code line :

Promise<Double> { completion, reject -> Void in

I am not able to identify why it's giving this error. Is there any swift expert who can help me this.

Thank you!

PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
  • 1
    Promise dont seems to be a function – Jonathan Dec 13 '18 at 05:54
  • Not used promise kit but just quickly go through the docs and I found there is Promise not Promise Ignore if I am wrong – Prashant Tukadiya Dec 13 '18 at 06:02
  • Sometimes when you have so many things to infer, xcode stops doing it (good thing is only per file basis), what you can do is help it by writing the type by yourself. Also on this case, I think you missed the `return`. Also it happens that since xcode could not infer everything, it stops analyzing the code so it could not give you the error about the missing return – rgkobashi Dec 13 '18 at 06:05
  • @rgkobashi I Have already tried return. Its not working. – PlusInfosys Dec 13 '18 at 06:40
  • hmm... have you try commenting everything except `Promise { ...` just to isolate the error. Also, on the documentation mention that your type should be `Thenable` and `CatchMixin`: https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L8 Perhaps is that, that your Double doesnt implement those protocols. – rgkobashi Dec 13 '18 at 06:57
  • @rgkobashi I tried commenting on everything. problem is with "completion, reject " if I use "_", its working fine. But I need completion and reject block. It was working fine with swift 2.0. – PlusInfosys Dec 13 '18 at 07:51
  • oh ok, well that is a progress. Since you mention it was working fine on swift 2.0 I am thinking you updated your SDKs, if that is the case perhaps this init method (`completion, reject -> Void` as param) doesnt exists on the new versions, I guess there should be an equivalence. I made a quick look on the documentation and could not see this kind of init. – rgkobashi Dec 13 '18 at 07:58
  • @rgkobash Thanks for the help. figured out the issue now. Its change in Promisekit 6 and above. Instead of "completion, reject ", I used "seal". seal.fulfill to replace completion and seal.reject to replace reject. Its working fine now. – PlusInfosys Dec 13 '18 at 09:10
  • Does this answer your question? [Unable to infer closure type in the current context](https://stackoverflow.com/questions/53092951/unable-to-infer-closure-type-in-the-current-context) – ScottyBlades May 24 '20 at 20:16
  • @ScottyBlades Sorry but that doesnt answer my question. problem i has was due to PromiseKit Version. Accepted answer her resolved my issue. – PlusInfosys May 25 '20 at 07:04
  • Its conceptually the same answer. There was a compilation error in the closure. – ScottyBlades May 25 '20 at 13:06
  • Its not same. error to me was not due to closure usage. it was an error due to PromiseKit version change. – PlusInfosys May 25 '20 at 14:13
  • Yes in the closure from `Promise { fulfill, reject -> Void in }` to `Promise { seal -> Void in }` – ScottyBlades May 25 '20 at 18:35

1 Answers1

14

In the current PromiseKit version this

Promise<T> { fulfill, reject -> Void in }

is changed to

Promise<T> { seal -> Void in }

So, your new implementation will change to this,

func calculateTravelTime(from: CLLocation, to: CLLocation) -> Promise<Double> {
     Promise<Double> { seal -> Void in
        let request = MKDirections.Request()
        request.transportType = .walking

        let fromPlacemark = MKPlacemark(coordinate: from.coordinate)
        let toPlacemark = MKPlacemark(coordinate: to.coordinate)

        let fromMapPoint = MKMapItem(placemark: fromPlacemark)
        let toMapPoint = MKMapItem(placemark: toPlacemark)

        request.source = fromMapPoint
        request.destination = toMapPoint
        request.requestsAlternateRoutes = false

        let directions = MKDirections(request: request)
        directions.calculate { response, error in
            if error != nil {
                seal.reject(error!)
            } else {
                let time = (response?.routes.first?.expectedTravelTime ?? 0) / 60.0
               seal.fulfill(time)
            }
        }

    }
} 
Kamran
  • 14,987
  • 4
  • 33
  • 51