CancellableThenable

public protocol CancellableThenable : AnyObject

CancellableThenable represents an asynchronous operation that can be both chained and cancelled. When chained, all CancellableThenable members of the chain are cancelled when cancel is called on the associated CancelContext.

  • Append the task and reject function for a cancellable task to the cancel context

    Declaration

    Swift

    func appendCancellableTask(task: CancellableTask?, reject: ((Error) -> Void)?)
  • appendCancelContext(from:) Extension method

    Append the cancel context associated with from to our cancel context. Typically from is a branch of our chain.

    Declaration

    Swift

    func appendCancelContext<Z>(from: Z) where Z : CancellableThenable
  • cancel(error:) Extension method

    Cancel all members of the promise chain and their associated asynchronous operations.

    Declaration

    Swift

    func cancel(error: Error? = nil)

    Parameters

    error

    Specifies the cancellation error to use for the cancel operation, defaults to PMKError.cancelled

  • isCancelled Extension method

    True if all members of the promise chain have been successfully cancelled, false otherwise.

    Declaration

    Swift

    var isCancelled: Bool { get }
  • cancelAttempted Extension method

    True if cancel has been called on the CancelContext associated with this promise, false otherwise. cancelAttempted will be true if cancel is called on any promise in the chain.

    Declaration

    Swift

    var cancelAttempted: Bool { get }
  • cancelledError Extension method

    The cancellation error generated when the promise is cancelled, or nil if not cancelled.

    Declaration

    Swift

    var cancelledError: Error? { get }
  • then(on:flags:_:) Extension method

    The provided closure executes when this cancellable promise resolves. This allows chaining cancellable promises.

    Declaration

    Swift

    func then<V: CancellableThenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (U.T) throws -> V) -> CancellablePromise<V.U.T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that executes when this cancellable promise fulfills. It must return a cancellable promise.

    Return Value

    A new cancellable promise that resolves when the cancellable promise returned from the provided closure resolves. For example:

    let context = firstly { URLSession.shared.dataTaskCC(.promise, with: url1) }.then { response in transform(data: response.data) // returns a CancellablePromise }.done { transformation in //… }.cancelContext

    //…

    context.cancel()

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

    The provided closure executes when this cancellable promise resolves. This allows chaining promises and cancellable promises.

    Declaration

    Swift

    func thenCC<V: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (U.T) throws -> V) -> CancellablePromise<V.T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that executes when this cancellable promise fulfills. It must return a promise (not a cancellable promise).

    Return Value

    A new cancellable promise that resolves when the promise returned from the provided closure resolves. For example:

    let context = firstly { URLSession.shared.dataTaskCC(.promise, with: url1) }.then { response in transform(data: response.data) // returns a Promise }.done { transformation in //… }.cancelContext

    //…

    context.cancel()

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

    The provided closure is executed when this cancellable promise is resolved.

    This is like then but it requires the closure to return a non-promise and non-cancellable-promise.

    Declaration

    Swift

    func map<V>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T) throws -> V) -> CancellablePromise<V>

    Parameters

    on

    The queue to which the provided closure dispatches.

    transform

    The closure that is executed when this CancellablePromise is fulfilled. It must return a non-promise and non-cancellable-promise.

    Return Value

    A new cancellable promise that is resolved with the value returned from the provided closure. For example:

    let context = firstly { URLSession.shared.dataTaskCC(.promise, with: url1) }.map { response in response.data.length }.done { length in //… }.cancelContext

    //…

    context.cancel()

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

    The provided closure is executed when this cancellable promise is resolved.

    In your closure return an Optional, if you return nil the resulting cancellable promise is rejected with PMKError.compactMap, otherwise the cancellable promise is fulfilled with the unwrapped value.

     let context = firstly {
         URLSession.shared.dataTaskCC(.promise, with: url)
     }.compactMap {
         try JSONSerialization.jsonObject(with: $0.data) as? [String: String]
     }.done { dictionary in
         //…
     }.catch {
         // either `PMKError.compactMap` or a `JSONError`
     }.cancelContext
    
     //…
    
     context.cancel()
    

    Declaration

    Swift

    func compactMap<V>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T) throws -> V?) -> CancellablePromise<V>
  • done(on:flags:_:) Extension method

    The provided closure is executed when this cancellable promise is resolved.

    Equivalent to map { x -> Void in, but since we force the Void return Swift is happier and gives you less hassle about your closure’s qualification.

    Declaration

    Swift

    func done(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (U.T) throws -> Void) -> CancellablePromise<Void>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed when this CancellablePromise is fulfilled.

    Return Value

    A new cancellable promise fulfilled as Void.

    let context = firstly { URLSession.shared.dataTaskCC(.promise, with: url) }.done { response in print(response.data) }.cancelContext

    //…

    context.cancel()

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

    The provided closure is executed when this cancellable promise is resolved.

    This is like done but it returns the same value that the handler is fed. get immutably accesses the fulfilled value; the returned CancellablePromise maintains that value.

    Declaration

    Swift

    func get(on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (U.T) throws -> Void) -> CancellablePromise<U.T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed when this CancellablePromise is fulfilled.

    Return Value

    A new cancellable promise that is resolved with the value that the handler is fed. For example:

    let context = firstly { .valueCC(1) }.get { foo in print(foo, is 1) }.done { foo in print(foo, is 1) }.done { foo in print(foo, is Void) }.cancelContext

    //…

    context.cancel()

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

    The provided closure is executed with cancellable promise result.

    This is like get but provides the Result of the CancellablePromise so you can inspect the value of the chain at this point without causing any side effects.

    promise.tap{ print($0) }.then{ /*…

    Declaration

    Swift

    func tap(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(PromiseKit.Result<U.T>) -> Void) -> CancellablePromise<U.T>

    Parameters

    on

    The queue to which the provided closure dispatches.

    body

    The closure that is executed with Result of CancellablePromise.

    Return Value

    A new cancellable promise that is resolved with the result that the handler is fed. For example:

  • asVoid() Extension method

    Declaration

    Swift

    func asVoid() -> CancellablePromise<Void>

    Return Value

    a new cancellable promise chained off this cancellable promise but with its value discarded.

  • error Extension method

    Declaration

    Swift

    var error: Error? { get }

    Return Value

    The error with which this cancellable promise was rejected; nil if this promise is not rejected.

  • isPending Extension method

    Declaration

    Swift

    var isPending: Bool { get }

    Return Value

    true if the cancellable promise has not yet resolved.

  • isResolved Extension method

    Declaration

    Swift

    var isResolved: Bool { get }

    Return Value

    true if the cancellable promise has resolved.

  • isFulfilled Extension method

    Declaration

    Swift

    var isFulfilled: Bool { get }

    Return Value

    true if the cancellable promise was fulfilled.

  • isRejected Extension method

    Declaration

    Swift

    var isRejected: Bool { get }

    Return Value

    true if the cancellable promise was rejected.

  • value Extension method

    Declaration

    Swift

    var value: U.T? { get }

    Return Value

    The value with which this cancellable promise was fulfilled or nil if this cancellable promise is pending or rejected.

  • firstValue Extension method

    Declaration

    Swift

    var firstValue: CancellablePromise<U.T.Iterator.Element> { get }

    Return Value

    a cancellable promise fulfilled with the first value of this Collection or, if empty, a promise rejected with PMKError.emptySequence.

  • firstValue(on:flags:where:) Extension method

    Undocumented

    Declaration

    Swift

    func firstValue(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, where test: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<U.T.Iterator.Element>
  • lastValue Extension method

    Declaration

    Swift

    var lastValue: CancellablePromise<U.T.Iterator.Element> { get }

    Return Value

    a cancellable promise fulfilled with the last value of this Collection or, if empty, a promise rejected with PMKError.emptySequence.

  • sortedValues(on:flags:) Extension method

    Declaration

    Swift

    func sortedValues(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil) -> CancellablePromise<[U.T.Iterator.Element]>

    Return Value

    a cancellable promise fulfilled with the sorted values of this Sequence.