According to migration guide PromiseKit 6.x changed his policy about catch
blocks. In PMK 4 catch
returned the promise it was attached to. Now catch
is a chain- terminator. I understand why these changes were made but...
In my codebase (connected with PK4) I take some advantages from that catch
returns promise.
func loginAndSync(withServerAddress address: String, port: String) -> Promise<()> {
synchronizationService.stopAllRunningSingleSynchronizations()
return
authorizationService.save(serverAddress: address, andPort: port)
.then {
self.synchronizationService.startSingleFullSynchronization()
}
.then {
self.authorizationService.markAsServerSynced()
}
.catch { error in
log.error(error)
_ = self.authorizationService.markAsServerUnsynced()
}
}
In this function I make some logic which is in some cases failable. In case of error catch
block should make some logic but I want to also send to loginAndSync
function`s caller a result of this promise (fulfill or reject). Above function can be called for example by ViewController and in ViewController I want to show for example Error or Success Dialog.
This is a reason where I need a two catch
es for one Promise-chain. One for authorizationService
and one for UI
.
Is there any workaround in PromiseKit6 to achieve this?
Edit
I found two solutions (workarounds). I have created two answers to separate them. Future readers can decide which one is better or provide new one.