Now I am migrating from PromiseKit to Concurrency. As I understood, I should replace Promise and Guarantee with Task. However, I cannot find a replacement for Promise<T>.pending(). Is there something similar in Concurrency?
For instance, I want to use Task in the code below:
import CoreLocation
import PromiseKit
class A: NSObject {
let locationManager = CLLocationManager()
let promiseAndResolver = Promise<CLLocation>.pending()
func f() {
locationManager.delegate = self
locationManager.requestLocation()
}
}
extension A: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
promiseAndResolver.resolver.fulfill(location)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
promiseAndResolver.resolver.reject(error)
}
}
class B {
let a = A()
func f() {
a.promiseAndResolver.promise.done {
print($0)
}.cauterize()
a.f()
}
}
let b = B()
b.f()
You don't need to use
Taskto do this. You can usewithCheckedThrowingContinuationto convert the methodfinto anasync throwsmethod. Then when you call it, a child task of the current task is automatically created and you canawaitit directly.Your
B.fmethod can also be rewritten as anasyncmethod (I don't know whatcauterizedoes):Side note: if you actually run this, you will get an error printed from the catch block, probably because location manager needs permission :)