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.
-
Delegate
thenablefor thisCancellableThenableDeclaration
Swift
var thenable: U { get } -
The
CancelContextassociated with thisCancellableThenableDeclaration
Swift
var cancelContext: CancelContext { get } -
Tracks the cancel items for this
CancellableThenable. These items are removed from the associatedCancelContextwhen the thenable resolves.Declaration
Swift
var cancelItemList: CancelItemList { get } -
appendCancellable(_:Extension methodreject: ) Append the
taskandrejectfunction for a cancellable task to the cancel contextDeclaration
Swift
func appendCancellable(_ cancellable: Cancellable?, reject: ((Error) -> Void)?) -
appendCancelContext(from:Extension method) Append the cancel context associated with
fromto our cancel context. Typicallyfromis 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
errorSpecifies the cancellation error to use for the cancel operation, defaults to
PMKError.cancelled -
isCancelledExtension methodTrue if all members of the promise chain have been successfully cancelled, false otherwise.
Declaration
Swift
var isCancelled: Bool { get } -
cancelAttemptedExtension methodTrue if
cancelhas been called on the CancelContext associated with this promise, false otherwise.cancelAttemptedwill be true ifcancelis called on any promise in the chain.Declaration
Swift
var cancelAttempted: Bool { get } -
cancelledErrorExtension methodThe cancellation error generated when the promise is cancelled, or
nilif 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 : CancellableThenableParameters
onThe dispatcher that executes the provided closure.
bodyThe 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 : ThenableParameters
onThe dispatcher that executes the provided closure.
bodyThe 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
thenbut 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
onThe queue to which the provided closure dispatches.
transformThe 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 returnnilthe resulting cancellable promise is rejected withPMKError.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 theVoidreturn 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
onThe dispatcher that executes the provided closure.
bodyThe 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
donebut it returns the same value that the handler is fed.getimmutably 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
onThe dispatcher that executes the provided closure.
bodyThe 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
getbut provides the Resultof 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
onThe dispatcher that executes the provided closure.
bodyThe 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 methodDeclaration
Swift
func asVoid() -> CancellablePromise<Void>Return Value
a new cancellable promise chained off this cancellable promise but with its value discarded.
-
errorExtension methodDeclaration
Swift
var error: Error? { get }Return Value
The error with which this cancellable promise was rejected;
nilif this promise is not rejected. -
isPendingExtension methodDeclaration
Swift
var isPending: Bool { get }Return Value
trueif the cancellable promise has not yet resolved. -
isResolvedExtension methodDeclaration
Swift
var isResolved: Bool { get }Return Value
trueif the cancellable promise has resolved. -
isFulfilledExtension methodDeclaration
Swift
var isFulfilled: Bool { get }Return Value
trueif the cancellable promise was fulfilled. -
isRejectedExtension methodDeclaration
Swift
var isRejected: Bool { get }Return Value
trueif the cancellable promise was rejected. -
valueExtension methodDeclaration
Swift
var value: U.T? { get }Return Value
The value with which this cancellable promise was fulfilled or
nilif this cancellable promise is pending or rejected. -
then(on:Extension methodflags: _: ) 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 : CancellableThenableParameters
onThe queue to which the provided closure dispatches.
flagsDispatchWorkItemFlagsto be applied when dispatching.bodyThe 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:Extension methodflags: _: ) 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 : ThenableParameters
onThe dispatcher that executes the provided closure.
flagsDispatchWorkItemFlagsto be applied when dispatching.bodyThe 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:Extension methodflags: _: ) The provided closure is executed when this cancellable promise is resolved.
This is like
thenbut 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
onThe queue to which the provided closure dispatches.
flagsDispatchWorkItemFlagsto be applied when dispatching.transformThe 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:Extension methodflags: _: ) The provided closure is executed when this cancellable promise is resolved.
In your closure return an
Optional, if you returnnilthe resulting cancellable promise is rejected withPMKError.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>
-
mapValues(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V]> -
flatMapValues(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.Iterator.Element]> where V : Sequence -
compactMapValues(on:Extension method_: ) 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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V?) -> CancellablePromise<[V]> -
thenMap(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T]> where V : CancellableThenable -
thenMap(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T]> where V : Thenable -
thenFlatMap(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T.Iterator.Element]> where V : CancellableThenable, V.U.T : Sequence -
thenFlatMap(on: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: Dispatcher = conf.D.map, _ transform: @escaping (U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T.Iterator.Element]> where V : Thenable, V.T : Sequence -
filterValues(on: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: Dispatcher = conf.D.map, _ isIncluded: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<[U.T.Iterator.Element]>
-
firstValueExtension methodDeclaration
Swift
var firstValue: CancellablePromise<U.T.Iterator.Element> { get }Return Value
a cancellable promise fulfilled with the first value of this
Collectionor, if empty, a promise rejected with PMKError.emptySequence. -
firstValue(on:Extension methodwhere: ) Undocumented
Declaration
Swift
func firstValue(on: Dispatcher = conf.D.map, where test: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<U.T.Iterator.Element> -
lastValueExtension methodDeclaration
Swift
var lastValue: CancellablePromise<U.T.Iterator.Element> { get }Return Value
a cancellable promise fulfilled with the last value of this
Collectionor, if empty, a promise rejected with PMKError.emptySequence.
-
sortedValues(on:Extension method) Declaration
Swift
func sortedValues(on: Dispatcher = conf.D.map) -> CancellablePromise<[U.T.Iterator.Element]>Return Value
a cancellable promise fulfilled with the sorted values of this
Sequence.
-
mapValues(on:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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 -
compactMapValues(on:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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:Extension methodflags: _: ) 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]>
-
firstValue(on:Extension methodflags: where: ) Undocumented
Declaration
Swift
func firstValue(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, where test: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<U.T.Iterator.Element>
-
sortedValues(on:Extension methodflags: ) 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.
View on GitHub
CancellableThenable Protocol Reference