CancellableCatchMixin
public protocol CancellableCatchMixin : CancellableThenable
Provides catch and recover to your object that conforms to CancellableThenable
-
catch(on:flags:policy:_:)Extension methodThe provided closure executes when this cancellable promise rejects.
Rejecting a promise cascades: rejecting all subsequent promises (unless recover is invoked) thus you will typically place your catch at the end of a chain. Often utility promises will not have a catch, instead delegating the error handling to the caller.
See also
CancellationDeclaration
Swift
@discardableResult func `catch`(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) -> Void) -> CancellableFinalizerParameters
onThe queue to which the provided closure dispatches.
policyThe default policy does not execute your handler for cancellation errors.
bodyThe handler to execute if this promise is rejected.
Return Value
A promise finalizer.
-
recover(on:flags:policy:_:)Extension methodThe provided closure executes when this cancellable promise rejects.
Unlike
catch,recovercontinues the chain. Userecoverin circumstances where recovering the chain from certain errors is a possibility. For example:let context = firstly { CLLocationManager.requestLocation() }.recover { error in guard error == CLError.unknownLocation else { throw error } return .value(CLLocation.chicago) }.cancelContext //… context.cancel()See also
CancellationDeclaration
Swift
func recover<V: CancellableThenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) throws -> V) -> CancellablePromise<M.T> where V.U.T == M.TParameters
onThe queue to which the provided closure dispatches.
policyThe default policy does not execute your handler for cancellation errors.
bodyThe handler to execute if this promise is rejected.
-
recoverCC(on:flags:policy:_:)Extension methodThe provided closure executes when this cancellable promise rejects.
Unlike
catch,recovercontinues the chain. Userecoverin circumstances where recovering the chain from certain errors is a possibility. For example:let context = firstly { CLLocationManager.requestLocation() }.recover { error in guard error == CLError.unknownLocation else { throw error } return .value(CLLocation.chicago) }.cancelContext //… context.cancel()See also
CancellationNote
Methods with theCCsuffix create a new CancellablePromise, and those without theCCsuffix accept an existing CancellablePromise.Declaration
Swift
func recoverCC<V: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) throws -> V) -> CancellablePromise<M.T> where V.T == M.TParameters
onThe queue to which the provided closure dispatches.
policyThe default policy does not execute your handler for cancellation errors.
bodyThe handler to execute if this promise is rejected.
-
recover(on:flags:cancelValue:_:)Extension methodThe provided closure executes when this promise rejects.
This variant of
recoverrequires the handler to return a CancellableGuarantee, thus it returns a CancellableGuarantee itself and your closure cannotthrow.Note
Note it is logically impossible for this to take acatchPolicy, thusallErrorsare handled.See also
CancellationDeclaration
Swift
@discardableResult func recover(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, cancelValue: M.T? = nil, _ body: @escaping(Error) -> CancellableGuarantee<M.T>) -> CancellableGuarantee<M.T>Parameters
onThe queue to which the provided closure dispatches.
cancelValueoptional override value to use when cancelled
bodyThe handler to execute if this promise is rejected.
-
recoverCC(on:flags:cancelValue:_:)Extension methodThe provided closure executes when this promise rejects.
This variant of
recoverrequires the handler to return a Guarantee, thus it returns a Guarantee itself and your closure cannotthrow.Note
Note it is logically impossible for this to take acatchPolicy, thusallErrorsare handled.See also
CancellationNote
Methods with theCCsuffix create a new CancellablePromise, and those without theCCsuffix accept an existing CancellablePromise.Declaration
Swift
@discardableResult func recoverCC(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, cancelValue: M.T? = nil, _ body: @escaping(Error) -> Guarantee<M.T>) -> CancellableGuarantee<M.T>Parameters
onThe queue to which the provided closure dispatches.
cancelValueoptional override value to use when cancelled
bodyThe handler to execute if this promise is rejected.
-
ensure(on:flags:_:)Extension methodThe provided closure executes when this cancellable promise resolves, whether it rejects or not.
let context = firstly { UIApplication.shared.networkActivityIndicatorVisible = true //… returns a cancellable promise }.done { //… }.ensure { UIApplication.shared.networkActivityIndicatorVisible = false }.catch { //… }.cancelContext //… context.cancel()Declaration
Swift
func ensure(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping () -> Void) -> CancellablePromise<M.T>Parameters
onThe queue to which the provided closure dispatches.
bodyThe closure that executes when this promise resolves.
Return Value
A new promise, resolved with this promise’s resolution.
-
ensureThen(on:flags:_:)Extension methodThe provided closure executes when this cancellable promise resolves, whether it rejects or not. The chain waits on the returned
CancellablePromise<Void>.let context = firstly { setup() // returns a cancellable promise }.done { //… }.ensureThen { teardown() // -> CancellablePromise<Void> }.catch { //… }.cancelContext //… context.cancel()Declaration
Swift
func ensureThen(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping () -> CancellablePromise<Void>) -> CancellablePromise<M.T>Parameters
onThe queue to which the provided closure dispatches.
bodyThe closure that executes when this promise resolves.
Return Value
A new cancellable promise, resolved with this promise’s resolution.
-
cauterize()Extension methodConsumes the Swift unused-result warning.
Note
You shouldcatch, but in situations where you know you don’t need acatch,cauterizemakes your intentions clear.Declaration
Swift
@discardableResult func cauterize() -> CancellableFinalizer
-
recover(on:flags:_:)Extension methodThe provided closure executes when this cancellable promise rejects.
This variant of
recoveris specialized forVoidpromises and de-errors your chain returning aCancellableGuarantee, thus you cannotthrowand you must handle all errors including cancellation.See also
CancellationDeclaration
Swift
@discardableResult func recover(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(Error) -> Void) -> CancellableGuarantee<Void>Parameters
onThe queue to which the provided closure dispatches.
bodyThe handler to execute if this promise is rejected.
-
recover(on:flags:policy:_:)Extension methodThe provided closure executes when this cancellable promise rejects.
This variant of
recoverensures that no error is thrown from the handler and allows specifying a catch policy.See also
CancellationDeclaration
Swift
func recover(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) throws -> Void) -> CancellablePromise<Void>Parameters
onThe queue to which the provided closure dispatches.
policyThe default policy does not execute your handler for cancellation errors.
bodyThe handler to execute if this promise is rejected.
View on GitHub
Install in Dash
CancellableCatchMixin Protocol Reference