TokenPromise
public struct TokenPromise<Value, Error>
extension TokenPromise: Equatable
A Promise adapter that automatically applies a PromiseInvalidationToken.
This exposes the same methods as Promise but it always passes a given
PromiseInvalidationToken to the underlying promise.
Important
TokenPromise automatically cancels any returned child Promises when the
PromiseInvalidationToken is invalidated. The only exception is Promises returned from
tap() or tap(on:_:), as these methods do not propagate cancellation.
A TokenPromise is created with the method Promise.withToken(_:). The wrapped Promise can
be accessed with the .inner property.
Example
methodReturningPromise()
.withToken(promiseToken)
.then({ (value) in
// handle value
}).catch({ (error) in
// handle error
})
-
The
PromiseInvalidationTokento use when invoking methods on the wrappedPromise.Declaration
Swift
public let token: PromiseInvalidationToken -
Returns a new
TokenPromisethat wraps the given promise.Declaration
Swift
public init(promise: Promise<Value, Error>, token: PromiseInvalidationToken)Parameters
promiseThe
Promiseto wrap.tokenThe
PromiseInvalidationTokento use when invoking methods on thepromise. -
Registers a callback that is invoked when the promise is fulfilled.
See also
Declaration
Swift
public func then(on context: PromiseContext = .auto, _ onSuccess: @escaping (Value) -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will resolve to the same value as the receiver. You may safely ignore this value.
-
Registers a callback that is invoked when the promise is fulfilled.
See also
Declaration
Swift
public func map<U>(on context: PromiseContext, _ onSuccess: @escaping (Value) -> U) -> TokenPromise<U, Error>Parameters
contextThe context to invoke the callback on.
onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will be fulfilled with the return value of
onSuccess. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
Registers a callback that is invoked when the promise is fulfilled.
See also
Declaration
Swift
public func flatMap<U>(on context: PromiseContext, _ onSuccess: @escaping (Value) -> Promise<U, Error>) -> TokenPromise<U, Error>Parameters
contextThe context to invoke the callback on.
onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will be eventually resolved using the promise returned from
onSuccess. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
Registers a callback that is invoked when the promise is rejected.
This method (or
always) should be used to terminate a promise chain.See also
Declaration
Swift
@discardableResult public func `catch`(on context: PromiseContext = .auto, _ onError: @escaping (Error) -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will resolve to the same value as the receiver. You may safely ignore this value.
-
See also
Declaration
Swift
public func recover(on context: PromiseContext, _ onError: @escaping (Error) -> Value) -> TokenPromise<Value, NoError>Parameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be fulfilled with the return value of
onError. If the receiver is fulfilled or cancelled, the returned promise will also be fulfilled or cancelled. -
See also
Declaration
Swift
public func mapError<E>(on context: PromiseContext, _ onError: @escaping (Error) -> E) -> TokenPromise<Value, E>Parameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be rejected with the return value of
onError. If the receiver is fulfilled or cancelled, the returned promise will also be fulfilled or cancelled. -
See also
Declaration
Swift
public func flatMapError<E>(on context: PromiseContext, _ onError: @escaping (Error) -> Promise<Value, E>) -> TokenPromise<Value, E>Parameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be eventually resolved using the promise returned from
onError. If the receiver is fulfilled or cancelled, the returned promise will also be fulfilled or cancelled. -
Declaration
Swift
public func tryMapError<E>(on context: PromiseContext, _ onError: @escaping (Error) throws -> E) -> TokenPromise<Value, Swift.Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be rejected with the return value of
onError, or is rejected ifonErrorthrows an error. If the receiver is fulfilled or cancelled, the returned promise will also be fulfilled or cancelled. -
Declaration
Swift
public func tryFlatMapError<E>(on context: PromiseContext, _ onError: @escaping (Error) throws -> Promise<Value, E>) -> TokenPromise<Value, Swift.Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be eventually resolved using the promise returned from
onError, or is rejected ifonErrorthrows an error. If the receiver is fulfilled or cancelled, the returned promise will also be fulfilled or cancelled. -
-
-
Registers a callback that will be invoked with the promise result, no matter what it is.
See also
Declaration
Swift
@discardableResult public func always(on context: PromiseContext = .auto, _ onComplete: @escaping (PromiseResult<Value, Error>) -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onCompleteThe callback that is invoked with the promise’s value.
Return Value
A new promise that will resolve to the same value as the receiver. You may safely ignore this value.
-
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new result.
See also
Declaration
Swift
public func mapResult<T, E>(on context: PromiseContext, onComplete: @escaping (PromiseResult<Value, Error>) -> PromiseResult<T, E>) -> TokenPromise<T, E>Parameters
contextThe context to invoke the callback on.
onCompleteThe callback that is invoked with the promise’s value. This callback returns a new result, which the returned promise will adopt the value of.
Return Value
A new promise that adopts the result returned by
onComplete. -
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new promise to wait on.
See also
Declaration
Swift
public func flatMapResult<T, E>(on context: PromiseContext, _ onComplete: @escaping (PromiseResult<Value, Error>) -> Promise<T, E>) -> TokenPromise<T, E>Parameters
contextThe context to invoke the callback on.
onCompleteThe callback that is invoked with the promise’s value. This callback returns a new promise, which the returned promise will adopt the value of.
Return Value
A new promise that adopts the same value that the promise returned by
onCompletedoes. -
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new result.
Declaration
Swift
public func tryMapResult<T, E>(on context: PromiseContext, _ onComplete: @escaping (PromiseResult<Value, Error>) throws -> PromiseResult<T, E>) -> TokenPromise<T, Swift.Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onCompleteThe callback that is invoked with the promise’s value. This callback returns a new result, which the returned promise will adopt the value of.
Return Value
A new promise that adopts the result returned by
onComplete, or is rejected ifonCompletethrows an error. -
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new promise to wait on.
Declaration
Swift
public func tryFlatMapResult<T, E>(on context: PromiseContext, _ onComplete: @escaping (PromiseResult<Value, Error>) throws -> Promise<T, E>) -> TokenPromise<T, Swift.Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onCompleteThe callback that is invoked with the promise’s value. This callback returns a new promise, which the returned promise will adopt the value of.
Return Value
A new
TokenPromisethat adopts the same value that the promise returned byonCompletedoes, or is rejected ifonCompletethrows an error. -
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new result.
-
Registers a callback that will be invoked with the promise result, no matter what it is, and returns a new promise to wait on.
-
Registers a callback that will be invoked when the promise is resolved without affecting behavior.
This is similar to an
alwayscallback except it doesn’t create a newPromiseand instead returns its receiver. This means it won’t delay any chained callbacks and it won’t affect automatic cancellation propagation behavior.This is similar to
tap().always(on:_:)except it can be inserted into any promise chain without affecting the chain.Note
This method is intended for inserting into the middle of a promise chain without affecting existing behavior (in particular, cancellation propagation). If you are not inserting this into the middle of a promise chain, you probably want to use
then(on:_:),map(on:_:),catch(on:_:), oralways(on:_:)instead.See also
Declaration
Swift
@discardableResult public func tap(on context: PromiseContext = .auto, _ onComplete: @escaping (PromiseResult<Value, Error>) -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onCompleteThe callback that is invoked with the promise’s value.
Return Value
The receiver.
-
Returns a new
TokenPromisethat adopts the result of the receiver without affecting its behavior.The returned
TokenPromisewill always resolve with the same value that its receiver does, but it won’t affect the timing of any of the receiver’s other observers and it won’t affect automatic cancellation propagation behavior.tap().always(on:_:)behaves the same astap(on:_:)except it returns aTokenPromisewrapping a newPromisewhereastap(on:_:)returns the receiver and can be inserted into any promise chain without affecting the chain.Note
This method is intended for inserting into the middle of a promise chain without affecting existing behavior (in particular, cancellation propagation). If you are not inserting this into the middle of a promise chain, you probably want to use
then(on:_:),map(on:_:),catch(on:_:), oralways(on:_:)instead.See also
Declaration
Swift
public func tap() -> TokenPromise<Value, Error>Return Value
A new
TokenPromisethat adopts the same result as the receiver. Requesting the new wrappedPromiseto cancel does nothing. -
Registers a callback that will be invoked when the promise is cancelled.
See also
Declaration
Swift
@discardableResult public func onCancel(on context: PromiseContext = .auto, _ onCancel: @escaping () -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onCancelThe callback that is invoked when the promise is cancelled.
Return Value
A new promise that will resolve to the same value as the receiver. You may safely ignore this value.
-
Passes the
TokenPromiseto a block and then returns theTokenPromisefor further chaining.This method exists to make it easy to add multiple children to the same
Promisein a promise chain.Note
Having multiple children on a singlePromisecan interfere with automatic cancellation propagation. You may want to usetap(on:_:)ortap()for your sibling children if you’re returning the result of the promise chain to a caller that may wish to cancel the chain.Example:
return urlSession.dataTaskAsPromise(for: url) .withToken(promiseToken) .fork({ $0.tap().then(on: .main, { analytics.recordNetworkLoad($0.response, for: url) }) }) .tryMap(on: .utility, { try JSONDecoder().decode(Model.self, from: $0.data) })Declaration
Swift
public func fork(_ handler: (TokenPromise) throws -> Void) rethrows -> TokenPromise -
Returns a new
TokenPromisethat adopts the value of the receiver but ignores cancel requests.This is primarily useful when returning a nested promise in a callback handler in order to unlink cancellation of the outer promise with the inner one. It can also be used to stop propagating cancel requests up the chain, e.g. if you want to implement something similar to
tap(on:token:_:).Note
This is similar to
tap()except it prevents the parent promise from being automatically cancelled due to cancel propagation from any observer.Note
The returned
TokenPromisewill still be cancelled if its parent promise is cancelled.See also
Declaration
Swift
public func ignoringCancel() -> TokenPromise<Value, Error>
-
Two
TokenPromises compare as equal if they represent the same promise.Two distinct
TokenPromises that are resolved to the same value compare as unequal.Declaration
Swift
public static func == (lhs: TokenPromise, rhs: TokenPromise) -> Bool
-
Registers a callback that is invoked when the promise is fulfilled.
Declaration
Swift
public func tryThen(on context: PromiseContext = .auto, _ onSuccess: @escaping (Value) throws -> Void) -> TokenPromise<Value, Error>Parameters
contextThe context to invoke the callback on. If not provided, defaults to
.auto, which evaluates to.mainwhen invoked on the main thread, otherwise.default.onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will resolve to the same value as the receiver, or rejected if
onSuccessthrows an error. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
Registers a callback that is invoked when the promise is fulfilled.
See also
Declaration
Swift
public func tryMap<U>(on context: PromiseContext, _ onSuccess: @escaping (Value) throws -> U) -> TokenPromise<U, Error>Parameters
contextThe context to invoke the callback on.
onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will be fulfilled with the return value of
onSuccess, or rejected ifonSuccessthrows an error. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
Registers a callback that is invoked when the promise is fulfilled.
See also
-
Registers a callback that is invoked when the promise is fulfilled.
See also
Declaration
Swift
public func tryFlatMap<U, E>(on context: PromiseContext, _ onSuccess: @escaping (Value) throws -> Promise<U, E>) -> TokenPromise<U, Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onSuccessThe callback that is invoked with the fulfilled value.
Return Value
A new promise that will be eventually resolved using the promise returned from
onSuccess, or rejected ifonSuccessthrows an error. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
Registers a callback that is invoked when the promise is rejected.
Unlike
catch(on:_:)this callback can recover from the error and return a new value.See also
Declaration
Swift
public func tryRecover(on context: PromiseContext, _ onError: @escaping (Error) throws -> Value) -> TokenPromise<Value,Error>Parameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be fulfilled with the return value of
onError, or rejected ifonErrorthrows an error. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled. -
See also
Declaration
Swift
public func tryFlatMapError<E>(on context: PromiseContext, _ onError: @escaping (Error) throws -> Promise<Value, E>) -> TokenPromise<Value, Error> where E : ErrorParameters
contextThe context to invoke the callback on.
onErrorThe callback that is invoked with the rejected error.
Return Value
A new promise that will be eventually resolved using the promise returned from
onError, or rejected ifonErrorthrows an error. If the receiver is rejected or cancelled, the returned promise will also be rejected or cancelled.
View on GitHub
TokenPromise Structure Reference