CatchMixin

public protocol CatchMixin : Thenable

Provides catch and recover to your object that conforms to Thenable

  • catch(on:policy:_:) Extension method

    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

    Cancellation

    Declaration

    Swift

    @discardableResult
    func `catch`(on: Dispatcher = conf.D.return, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (Error) -> Void) -> PMKFinalizer

    Parameters

    on

    The dispatcher that executes the provided closure.

    policy

    The default policy does not execute your handler for cancellation errors.

    body

    The handler to execute if this promise is rejected.

    Return Value

    A promise finalizer.

  • catch(only:on:_:) Extension method

    The provided closure executes when this promise rejects with the specific error passed in. A final catch is still required at the end of the chain.

    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.

    Note

    Since this method handles only specific errors, supplying a CatchPolicy is unsupported.

    See also

    Cancellation

    Declaration

    Swift

    func `catch`<E>(only: E, on: Dispatcher = conf.D.return, _ body: @escaping (E) -> Void) -> PMKCascadingFinalizer where E : Equatable, E : Error

    Parameters

    only

    The specific error to be caught and handled (e.g., PMKError.emptySequence).

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected with the provided error.

    Return Value

    A promise finalizer that accepts additional catch clauses.

  • catch(only:on:policy:_:) Extension method

    The provided closure executes when this promise rejects with an error of the type passed in. A final catch is still required at the end of the chain.

    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

    Cancellation

    Declaration

    Swift

    func `catch`<E>(only: E.Type, on: Dispatcher = conf.D.return, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (E) -> Void) -> PMKCascadingFinalizer where E : Error

    Parameters

    only

    The error type to be caught and handled (e.g., PMKError).

    on

    The dispatcher that executes the provided closure.

    policy

    A CatchPolicy that further constrains the errors this handler will see. E.g., if you are receiving PMKError errors, do you want to see even those that result from cancellation?

    body

    The handler to execute if this promise is rejected with the provided error type.

    Return Value

    A promise finalizer that accepts additional catch clauses.

  • recover(on:policy:_:) Extension method

    The provided closure executes when this promise rejects.

    Unlike catch, recover continues the chain. It can return a replacement promise or rethrow. Use recover 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

    Cancellation

    Declaration

    Swift

    func recover<U>(on: Dispatcher = conf.D.map, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (Error) throws -> U) -> Promise<T> where U : Thenable, Self.T == U.T

    Parameters

    on

    The dispatcher that executes the provided closure.

    policy

    The default policy does not execute your handler for cancellation errors.

    body

    The handler to execute if this promise is rejected.

  • recover(only:on:_:) Extension method

    The provided closure executes when this promise rejects with the specific error passed in.

    Unlike catch, recover continues the chain. It can return a replacement promise or rethrow. Use recover in circumstances where recovering the chain from certain errors is a possibility. For example:

    firstly {
        CLLocationManager.requestLocation()
    }.recover(CLError.unknownLocation) {
        return .value(CLLocation.chicago)
    }
    

    Note

    Since this method recovers only specific errors, supplying a CatchPolicy is unsupported.

    See also

    Cancellation

    Declaration

    Swift

    func recover<U, E>(only: E, on: Dispatcher = conf.D.map, _ body: @escaping (E) throws -> U) -> Promise<T> where U : Thenable, E : Equatable, E : Error, Self.T == U.T

    Parameters

    only

    The specific error to be recovered (e.g., PMKError.emptySequence)

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected with the provided error.

  • recover(only:on:policy:_:) Extension method

    The provided closure executes when this promise rejects with an error of the type passed in.

    Unlike catch, recover continues the chain. It can return a replacement promise or rethrow. Use recover in circumstances where recovering the chain from certain errors is a possibility. For example:

    firstly {
        API.fetchData()
    }.recover(FetchError.self) { error in
        guard case .missingImage(let partialData) = error else { throw error }
        //…
        return .value(dataWithDefaultImage)
    }
    

    See also

    Cancellation

    Declaration

    Swift

    func recover<U, E>(only: E.Type, on: Dispatcher = conf.D.map, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (E) throws -> U) -> Promise<T> where U : Thenable, E : Error, Self.T == U.T

    Parameters

    only

    The error type to be recovered (e.g., PMKError).

    on

    The dispatcher that executes the provided closure.

    policy

    The default policy does not execute your handler for cancellation errors.

    body

    The handler to execute if this promise is rejected with the provided error type.

  • recover(on:_:) Extension method

    The provided closure executes when this promise rejects. This variant of recover requires the handler to return a Guarantee; your closure cannot throw.

    It is logically impossible for this variant to accept a catchPolicy. All errors will be presented to your closure for processing.

    See also

    Cancellation

    Declaration

    Swift

    @discardableResult
    func recover(on: Dispatcher = conf.D.map, _ body: @escaping (Error) -> Guarantee<T>) -> Guarantee<T>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected.

  • ensure(on:_:) Extension method

    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

    Swift

    func ensure(on: Dispatcher = conf.D.return, _ body: @escaping () -> Void) -> Promise<T>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The closure that executes when this promise resolves.

    Return Value

    A new promise, resolved with this promise’s resolution.

  • ensureThen(on:_:) Extension method

    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

    Swift

    func ensureThen(on: Dispatcher = conf.D.return, _ body: @escaping () -> Guarantee<Void>) -> Promise<T>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The closure that executes when this promise resolves.

    Return Value

    A new promise, resolved with this promise’s resolution.

  • cauterize() Extension method

    Consumes the Swift unused-result warning.

    Note

    You should catch, but in situations where you know you don’t need a catch, cauterize makes your intentions clear.

    Declaration

    Swift

    @discardableResult
    func cauterize() -> PMKFinalizer
  • recover(on:flags:_:) Extension method

    The provided closure executes when this promise rejects. This variant of recover requires the handler to return a Guarantee; your closure cannot throw.

    It is logically impossible for this variant to accept a catchPolicy. All errors will be presented to your closure for processing.

    See also

    Cancellation

    Declaration

    Swift

    @discardableResult
    func recover(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (Error) -> Guarantee<T>) -> Guarantee<T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    flags

    DispatchWorkItemFlags to be applied when dispatching.

    body

    The handler to execute if this promise is rejected.

Available where T == Void

  • recover(on:_:) Extension method

    The provided closure executes when this promise rejects.

    This variant of recover is specialized for Void promises and de-errors your chain, returning a Guarantee. Thus, you cannot throw and you must handle all error types, including cancellation.

    See also

    Cancellation

    Declaration

    Swift

    @discardableResult
    func recover(on: Dispatcher = conf.D.map, _ body: @escaping (Error) -> Void) -> Guarantee<Void>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected.

  • recover(on:policy:_:) Extension method

    The provided closure executes when this promise rejects.

    This variant of recover ensures that no error is thrown from the handler and allows you to specify a catch policy.

    See also

    Cancellation

    Declaration

    Swift

    func recover(on: Dispatcher = conf.D.map, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (Error) throws -> Void) -> Promise<Void>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected.

  • recover(only:on:_:) Extension method

    The provided closure executes when this promise rejects with the specific error passed in.

    Unlike catch, recover continues the chain. It can return a replacement promise or rethrow. Use recover in circumstances where recovering the chain from certain errors is a possibility.

    Note

    Since this method recovers only specific errors, supplying a CatchPolicy is unsupported.

    See also

    Cancellation

    Declaration

    Swift

    func recover<E>(only: E, on: Dispatcher = conf.D.map, _ body: @escaping (E) throws -> Void) -> Promise<Void> where E : Equatable, E : Error

    Parameters

    only

    The specific error to be recovered (e.g., PMKError.emptySequence)

    on

    The dispatcher that executes the provided closure.

    body

    The handler to execute if this promise is rejected with the provided error.

  • recover(only:on:policy:_:) Extension method

    The provided closure executes when this promise rejects with an error of the type passed in.

    Unlike catch, recover continues the chain. It can return a replacement promise or rethrow. Use recover in circumstances where recovering the chain from certain errors is a possibility.

    See also

    Cancellation

    Declaration

    Swift

    func recover<E>(only: E.Type, on: Dispatcher = conf.D.map, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping (E) throws -> Void) -> Promise<Void> where E : Error

    Parameters

    only

    The error type to be recovered (e.g., PMKError).

    on

    The dispatcher that executes the provided closure.

    policy

    The default policy does not execute your handler for cancellation errors.

    body

    The handler to execute if this promise is rejected with the provided error type.

  • recover(on:flags:_:) Extension method

    The provided closure executes when this promise rejects.

    This variant of recover is specialized for Void promises and de-errors your chain, returning a Guarantee. Thus, you cannot throw and you must handle all error types, including cancellation.

    See also

    Cancellation

    Declaration

    Swift

    @discardableResult
    func recover(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (Error) -> Void) -> Guarantee<Void>

    Parameters

    on

    The queue to which the provided closure dispatches.

    flags

    DispatchWorkItemFlags to be applied when dispatching.

    body

    The handler to execute if this promise is rejected.