Structures
The following structures are available globally.
-
DelayedPromise
is like aPromise
but it doesn’t invoke its callback until the.promise
variable is accessed.The purpose of
DelayedPromise
is to allow functions to return calculations that aren’t performed if they’re not needed.Example:
See morefunc getUserInfo() -> (name: String, avatar: DelayedPromise<UIImage,Error>) { … } let (name, avatar) = getUserInfo() nameLabel.text = name avatar.promise.then { [weak self] (image) in self?.imageView.image = image }
Declaration
Swift
public struct DelayedPromise<Value, Error>
-
A
Promise
is a construct that will eventually hold a value or error, and can invoke callbacks when that happens.Example usage:
Promise(on: .utility) { resolver in let value = try someLongComputation() resolver.fulfill(with: value) }.then(on: main) { value in self.updateUI(with: value) }.catch(on: .main) { error in self.handleError(error) }
Promises can also be cancelled. With a
See morePromise
object you can invoke.requestCancel()
, which is merely advisory; the promise does not have to actually implement cancellation and may resolve anyway. But if a promise does implement cancellation, it can then callresolver.cancel()
. Note that even if the promise supports cancellation, calling.requestCancel()
on an unresolved promise does not guarantee that it will cancel, as the promise may be in the process of resolving when that method is invoked. Make sure to use the invalidation token support if you need to ensure your registered callbacks aren’t invoked past a certain point.Declaration
Swift
public struct Promise<Value, Error>
-
A type that can be used to cancel a promise without holding onto the full promise.
In particular, this acts like a weak reference, allowing for cancelling the promise without creating a retain cycle. Promise retain cycles normally break themselves anyway when the promise is resolved, but a misbehaving promise body may drop the resolver without ever resolving the promise. If the
Promise
has no more references to it this automatically cancels the promise, but a retain cycle prevents this.This is returned from
Promise.cancellable
.Declaration
Swift
public struct PromiseCancellable
-
A
Promise
adapter that automatically applies aPromiseInvalidationToken
.This exposes the same methods as
Promise
but it always passes a givenPromiseInvalidationToken
to the underlying promise.Important
TokenPromise
automatically cancels any returned childPromise
s when thePromiseInvalidationToken
is invalidated. The only exception isPromise
s returned fromtap()
ortap(on:_:)
, as these methods do not propagate cancellation.A
TokenPromise
is created with the methodPromise.withToken(_:)
. The wrappedPromise
can be accessed with the.inner
property.Example
See moremethodReturningPromise() .withToken(promiseToken) .then({ (value) in // handle value }).catch({ (error) in // handle error })
Declaration
Swift
public struct TokenPromise<Value, Error>