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.

  • U

    Type of the delegate thenable

    Declaration

    Swift

    associatedtype U : Thenable
  • Delegate thenable for this CancellableThenable

    Declaration

    Swift

    var thenable: U { get }
  • The CancelContext associated with this CancellableThenable

    Declaration

    Swift

    var cancelContext: CancelContext { get }
  • Tracks the cancel items for this CancellableThenable. These items are removed from the associated CancelContext when the thenable resolves.

    Declaration

    Swift

    var cancelItemList: CancelItemList { get }
  • appendCancellable(_:reject:) Extension method

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

    Declaration

    Swift

    func appendCancellable(_ cancellable: Cancellable?, 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(with:) Extension method

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

    Declaration

    Swift

    func cancel(with error: Error = PMKError.cancelled)

    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:_:) Extension method

    The provided closure executes when this cancellable promise resolves.

    This allows chaining promises. The cancellable promise returned by the provided closure is resolved before the cancellable promise returned by this closure resolves.

    Declaration

    Swift

    func then<V>(on: Dispatcher = conf.D.map, _ body: @escaping (U.T) throws -> V) -> CancellablePromise<V.U.T> where V : CancellableThenable

    Parameters

    on

    The dispatcher that executes the provided closure.

    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.dataTask(.promise, with: url1) }.cancellize().then { response in transform(data: response.data) // returns a CancellablePromise }.done { transformation in //… }.cancelContext

    //…

    context.cancel()

  • then(on:_:) Extension method

    The provided closure executes when this cancellable promise resolves.

    This allows chaining promises. The promise returned by the provided closure is resolved before the cancellable promise returned by this closure resolves.

    Declaration

    Swift

    func then<V>(on: Dispatcher = conf.D.map, _ body: @escaping (U.T) throws -> V) -> CancellablePromise<V.T> where V : Thenable

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The closure that executes when this 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.dataTask(.promise, with: url1) }.cancellize().then { response in transform(data: response.data) // returns a Promise }.done { transformation in //… }.cancelContext

    //…

    context.cancel()

  • map(on:_:) 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: Dispatcher = conf.D.map, _ 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.dataTask(.promise, with: url1) }.cancellize().map { response in response.data.length }.done { length in //… }.cancelContext

    //…

    context.cancel()

  • compactMap(on:_:) 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.dataTask(.promise, with: url)
     }.cancellize().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: Dispatcher = conf.D.map, _ transform: @escaping (U.T) throws -> V?) -> CancellablePromise<V>
  • done(on:_:) 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: Dispatcher = conf.D.return, _ body: @escaping (U.T) throws -> Void) -> CancellablePromise<Void>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The closure that is executed when this promise is fulfilled.

    Return Value

    A new cancellable promise fulfilled as Void.

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

    //…

    context.cancel()

  • get(on:_:) 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: Dispatcher = conf.D.return, _ body: @escaping (U.T) throws -> Void) -> CancellablePromise<U.T>

    Parameters

    on

    The dispatcher that executes the provided closure.

    body

    The closure that is executed when this promise is fulfilled.

    Return Value

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

    let context = firstly { cancellize(Promise.value(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:_:) 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: Dispatcher = conf.D.map, _ body: @escaping (Result<U.T, Error>) -> Void) -> CancellablePromise<U.T>

    Parameters

    on

    The dispatcher that executes the provided closure.

    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.

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

    The provided closure executes when this cancellable promise resolves.

    This allows chaining promises. The cancellable promise returned by the provided closure is resolved before the cancellable promise returned by this closure resolves.

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

    Declaration

    Swift

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

    Parameters

    on

    The queue to which the provided closure dispatches.

    flags

    DispatchWorkItemFlags to be applied when dispatching.

    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 promise returned from the provided closure resolves.

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

    The provided closure executes when this cancellable promise resolves.

    This allows chaining promises. The promise returned by the provided closure is resolved before the cancellable promise returned by this closure resolves.

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

    Declaration

    Swift

    @_disfavoredOverload
    func then<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (U.T) throws -> V) -> CancellablePromise<V.T> where V : Thenable

    Parameters

    on

    The dispatcher that executes the provided closure.

    flags

    DispatchWorkItemFlags to be applied when dispatching.

    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.

  • 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.

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

    Declaration

    Swift

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

    Parameters

    on

    The queue to which the provided closure dispatches.

    flags

    DispatchWorkItemFlags to be applied when dispatching.

    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.

  • 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.dataTask(.promise, with: url)
    }.cancellize().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

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

Available where U.T: Sequence

Available where U.T: Collection

  • 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:where:) Extension method

    Undocumented

    Declaration

    Swift

    func firstValue(on: Dispatcher = conf.D.map, 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.

Available where U.T: Sequence, U.T.Iterator.Element: Comparable

Available where U.T: Sequence

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

    CancellablePromise<[U.T]> => U.T -> V => CancellablePromise<[V]>

    firstly {
        cancellize(Promise.value([1,2,3]))
    }.mapValues { integer in
        integer * 2
    }.done {
        // $0 => [2,4,6]
    }
    

    Declaration

    Swift

    func mapValues<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V]>
  • flatMapValues(on:flags:_:) Extension method

    CancellablePromise<[U.T]> => U.T -> [V] => CancellablePromise<[V]>

    firstly {
        cancellize(Promise.value([1,2,3]))
    }.flatMapValues { integer in
        [integer, integer]
    }.done {
        // $0 => [1,1,2,2,3,3]
    }
    

    Declaration

    Swift

    func flatMapValues<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.Iterator.Element]> where V : Sequence
  • CancellablePromise<[U.T]> => U.T -> V? => CancellablePromise<[V]>

    firstly {
        cancellize(Promise.value(["1","2","a","3"]))
    }.compactMapValues {
        Int($0)
    }.done {
        // $0 => [1,2,3]
    }
    

    Declaration

    Swift

    func compactMapValues<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V?) -> CancellablePromise<[V]>
  • thenMap(on:flags:_:) Extension method

    CancellablePromise<[U.T]> => U.T -> CancellablePromise<V> => CancellablePromise<[V]>

    firstly {
        cancellize(Promise.value([1,2,3]))
    }.thenMap { integer in
        cancellize(Promise.value(integer * 2))
    }.done {
        // $0 => [2,4,6]
    }
    

    Declaration

    Swift

    func thenMap<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T]> where V : CancellableThenable
  • thenMap(on:flags:_:) Extension method

    CancellablePromise<[U.T]> => U.T -> Promise<V> => CancellablePromise<[V]>

    firstly {
        Promise.value([1,2,3])
    }.cancellize().thenMap { integer in
        .value(integer * 2)
    }.done {
        // $0 => [2,4,6]
    }
    

    Declaration

    Swift

    func thenMap<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T]> where V : Thenable
  • thenFlatMap(on:flags:_:) Extension method

    CancellablePromise<[T]> => T -> CancellablePromise<[U]> => CancellablePromise<[U]>

    firstly {
        cancellize(Promise.value([1,2,3]))
    }.thenFlatMap { integer in
        cancellize(Promise.value([integer, integer]))
    }.done {
        // $0 => [1,1,2,2,3,3]
    }
    

    Declaration

    Swift

    func thenFlatMap<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T.Iterator.Element]> where V : CancellableThenable, V.U.T : Sequence
  • thenFlatMap(on:flags:_:) Extension method

    CancellablePromise<[T]> => T -> Promise<[U]> => CancellablePromise<[U]>

    firstly {
        Promise.value([1,2,3])
    }.cancellize().thenFlatMap { integer in
        .value([integer, integer])
    }.done {
        // $0 => [1,1,2,2,3,3]
    }
    

    Declaration

    Swift

    func thenFlatMap<V>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T.Iterator.Element]> where V : Thenable, V.T : Sequence
  • filterValues(on:flags:_:) Extension method

    CancellablePromise<[T]> => T -> Bool => CancellablePromise<[U]>

    firstly {
        cancellize(Promise.value([1,2,3]))
    }.filterValues {
        $0 > 1
    }.done {
        // $0 => [2,3]
    }
    

    Declaration

    Swift

    func filterValues(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ isIncluded: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<[U.T.Iterator.Element]>

Available where U.T: Collection

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

    Undocumented

    Declaration

    Swift

    func firstValue(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, where test: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<U.T.Iterator.Element>

Available where U.T: Sequence, U.T.Iterator.Element: Comparable

  • sortedValues(on:flags:) Extension method

    Declaration

    Swift

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

    Return Value

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