Just started using PromiseKit 6 with Swift. I've checked some examples and read the documentation. I have a scenario that seems trivial but I can't get around it. I have the following scenario:
func addModel(_ model: Model, image: UIImage?, completion: @escaping (Error?) -> Void) {
var modelToSet = model
var id = ""
firstly {
serviceWrapper.setImage(image!)
}.map { path in
modelToSet.imagePath = path
}.then { [serviceWrapper]
serviceWrapper.setModel(modelToSet)
}.map { documentId in
id = documentId
}.then {
CLLocationManager.promise()
}.done { [serviceWrapper] location in
serviceWrapper.setLocation(GeoPoint(latitude: location.lat, longitude: location.long), id: id)
}.catch { error in
completion(error)
}
}
As you can see, in this pseudo code I am force unwrapping the image. Any ideas how I can start the chain with the setImage method only if there is an image passed? Otherwise start from setModel directly. Or probably just return empty string if there is no image? What am I missing here?
After asking the PromiseKit community, I got what I needed. So this is how you can go with solving it with a local function:
func addModel(_ model: Model, image: UIImage?, completion: @escaping (Error?) -> Void) {
var modelToSet = model
var id = ""
func start() -> Promise<Void> {
guard let image = image else {
return Promise()
}
return serviceWrapper.uploadImage(image).map { path in
dishToSet.imagePath = path
}
}
firstly {
start()
}.then { [serviceWrapper]
serviceWrapper.setModel(modelToSet)
}.map { documentId in
id = documentId
}.then {
CLLocationManager.promise()
}.done { [serviceWrapper] location in
serviceWrapper.setLocation(GeoPoint(latitude: location.lat, longitude: location.long), id: id)
}.catch { error in
completion(error)
}
}