-
catch(on:
Extension methodflags: policy: _: ) The provided closure executes when this 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) -> PMKFinalizer
Parameters
on
The queue to which the provided closure dispatches.
policy
The default policy does not execute your handler for cancellation errors.
execute
The handler to execute if this promise is rejected.
Return Value
A promise finalizer.
-
recover(on:
Extension methodflags: policy: _: ) The provided closure executes when this promise rejects.
Unlike
catch
,recover
continues the chain. Userecover
in circumstances where recovering the chain from certain errors is a possibility. For example:firstly { CLLocationManager.requestLocation() }.recover { error in guard error == CLError.unknownLocation else { throw error } return .value(CLLocation.chicago) }
See also
CancellationDeclaration
Swift
func recover<U>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (Error) throws -> U) -> Promise<T> where U : Thenable, Self.T == U.T
Parameters
on
The queue to which the provided closure dispatches.
body
The handler to execute if this promise is rejected.
-
recover(on:
Extension methodflags: _: ) The provided closure executes when this promise rejects. This variant of
recover
requires 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
, thusallErrors
are handled.See also
CancellationDeclaration
Parameters
on
The queue to which the provided closure dispatches.
body
The handler to execute if this promise is rejected.
-
ensure(on:
Extension methodflags: _: ) The provided closure executes when this promise resolves, whether it rejects or not.
firstly { UIApplication.shared.networkActivityIndicatorVisible = true }.done { //… }.ensure { UIApplication.shared.networkActivityIndicatorVisible = false }.catch { //… }
Declaration
Parameters
on
The queue to which the provided closure dispatches.
body
The closure that executes when this promise resolves.
Return Value
A new promise, resolved with this promise’s resolution.
-
ensureThen(on:
Extension methodflags: _: ) The provided closure executes when this promise resolves, whether it rejects or not. The chain waits on the returned
Guarantee<Void>
.firstly { setup() }.done { //… }.ensureThen { teardown() // -> Guarante<Void> }.catch { //… }
Declaration
Parameters
on
The queue to which the provided closure dispatches.
body
The closure that executes when this promise resolves.
Return Value
A new 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
,cauterize
makes your intentions clear.Declaration
Swift
@discardableResult func cauterize() -> PMKFinalizer
-
recover(on:
Extension methodflags: _: ) The provided closure executes when this promise rejects.
This variant of
recover
is specialized forVoid
promises and de-errors your chain returning aGuarantee
, thus you cannotthrow
and you must handle all errors including cancellation.See also
CancellationDeclaration
Parameters
on
The queue to which the provided closure dispatches.
body
The handler to execute if this promise is rejected.
-
recover(on:
Extension methodflags: policy: _: ) The provided closure executes when this promise rejects.
This variant of
recover
ensures 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) -> Promise<Void>
Parameters
on
The queue to which the provided closure dispatches.
body
The handler to execute if this promise is rejected.