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.
-
Type of the delegate
thenable
Declaration
Swift
associatedtype U : Thenable
-
Delegate
thenable
for thisCancellableThenable
Declaration
Swift
var thenable: U { get }
-
The
CancelContext
associated with thisCancellableThenable
Declaration
Swift
var cancelContext: CancelContext { get }
-
Tracks the cancel items for this
CancellableThenable
. These items are removed from the associatedCancelContext
when the thenable resolves.Declaration
Swift
var cancelItemList: CancelItemList { get }
-
appendCancellableTask(task:reject:)
Extension methodAppend the
task
andreject
function for a cancellable task to the cancel contextDeclaration
Swift
func appendCancellableTask(task: CancellableTask?, reject: ((Error) -> Void)?)
-
appendCancelContext(from:)
Extension methodAppend the cancel context associated with
from
to our cancel context. Typicallyfrom
is a branch of our chain.Declaration
Swift
func appendCancelContext<Z>(from: Z) where Z : CancellableThenable
-
cancel(error:)
Extension methodCancel 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 methodTrue if all members of the promise chain have been successfully cancelled, false otherwise.
Declaration
Swift
var isCancelled: Bool { get }
-
cancelAttempted
Extension methodTrue if
cancel
has been called on the CancelContext associated with this promise, false otherwise.cancelAttempted
will be true ifcancel
is called on any promise in the chain.Declaration
Swift
var cancelAttempted: Bool { get }
-
cancelledError
Extension methodThe cancellation error generated when the promise is cancelled, or
nil
if not cancelled.Declaration
Swift
var cancelledError: Error? { get }
-
then(on:flags:_:)
Extension methodThe 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 methodThe 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 methodThe 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 methodThe provided closure is executed when this cancellable promise is resolved.
In your closure return an
Optional
, if you returnnil
the resulting cancellable promise is rejected withPMKError.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 methodThe provided closure is executed when this cancellable promise is resolved.
Equivalent to
map { x -> Void in
, but since we force theVoid
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 methodThe 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 methodThe provided closure is executed with cancellable promise result.
This is like
get
but 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: 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 methodDeclaration
Swift
func asVoid() -> CancellablePromise<Void>
Return Value
a new cancellable promise chained off this cancellable promise but with its value discarded.
-
error
Extension methodDeclaration
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 methodDeclaration
Swift
var isPending: Bool { get }
Return Value
true
if the cancellable promise has not yet resolved. -
isResolved
Extension methodDeclaration
Swift
var isResolved: Bool { get }
Return Value
true
if the cancellable promise has resolved. -
isFulfilled
Extension methodDeclaration
Swift
var isFulfilled: Bool { get }
Return Value
true
if the cancellable promise was fulfilled. -
isRejected
Extension methodDeclaration
Swift
var isRejected: Bool { get }
Return Value
true
if the cancellable promise was rejected. -
value
Extension methodDeclaration
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.
-
mapValues(on:flags:_:)
Extension methodCancellablePromise<[U.T]>
=>U.T
->V
=>CancellablePromise<[V]>
firstly { .valueCC([1,2,3]) }.mapValues { integer in integer * 2 }.done { // $0 => [2,4,6] }
Declaration
Swift
func mapValues<V>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V]>
-
flatMapValues(on:flags:_:)
Extension methodCancellablePromise<[U.T]>
=>U.T
->[V]
=>CancellablePromise<[V]>
firstly { .valueCC([1,2,3]) }.flatMapValues { integer in [integer, integer] }.done { // $0 => [1,1,2,2,3,3] }
Declaration
Swift
func flatMapValues<V: Sequence>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.Iterator.Element]>
-
compactMapValues(on:flags:_:)
Extension methodCancellablePromise<[U.T]>
=>U.T
->V?
=>CancellablePromise<[V]>
firstly { .valueCC(["1","2","a","3"]) }.compactMapValues { Int($0) }.done { // $0 => [1,2,3] }
Declaration
Swift
func compactMapValues<V>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V?) -> CancellablePromise<[V]>
-
thenMap(on:flags:_:)
Extension methodCancellablePromise<[U.T]>
=>U.T
->CancellablePromise<V>
=>CancellablePromise<[V]>
firstly { .valueCC([1,2,3]) }.thenMap { integer in .valueCC(integer * 2) }.done { // $0 => [2,4,6] }
Declaration
Swift
func thenMap<V: CancellableThenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T]>
-
thenMapCC(on:flags:_:)
Extension methodCancellablePromise<[U.T]>
=>U.T
->Promise<V>
=>CancellablePromise<[V]>
firstly { .valueCC([1,2,3]) }.thenMap { integer in .value(integer * 2) }.done { // $0 => [2,4,6] }
Declaration
Swift
func thenMapCC<V: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T]>
-
thenFlatMap(on:flags:_:)
Extension methodCancellablePromise<[T]>
=>T
->CancellablePromise<[U]>
=>CancellablePromise<[U]>
firstly { .valueCC([1,2,3]) }.thenFlatMap { integer in .valueCC([integer, integer]) }.done { // $0 => [1,1,2,2,3,3] }
Declaration
Swift
func thenFlatMap<V: CancellableThenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.U.T.Iterator.Element]> where V.U.T: Sequence
-
thenFlatMapCC(on:flags:_:)
Extension methodCancellablePromise<[T]>
=>T
->Promise<[U]>
=>CancellablePromise<[U]>
firstly { .valueCC([1,2,3]) }.thenFlatMap { integer in .value([integer, integer]) }.done { // $0 => [1,1,2,2,3,3] }
Declaration
Swift
func thenFlatMapCC<V: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(U.T.Iterator.Element) throws -> V) -> CancellablePromise<[V.T.Iterator.Element]> where V.T: Sequence
-
filterValues(on:flags:_:)
Extension methodCancellablePromise<[T]>
=>T
-> Bool =>CancellablePromise<[U]>
firstly { .valueCC([1,2,3]) }.filterValues { $0 > 1 }.done { // $0 => [2,3] }
Declaration
Swift
func filterValues(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, _ isIncluded: @escaping (U.T.Iterator.Element) -> Bool) -> CancellablePromise<[U.T.Iterator.Element]>
-
firstValue
Extension methodDeclaration
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 methodUndocumented
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 methodDeclaration
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 methodDeclaration
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
.