Structures
The following structures are available globally.
-
DelayedPromiseis like aPromisebut it doesn’t invoke its callback until the.promisevariable is accessed.The purpose of
DelayedPromiseis 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> : Equatable -
A
Promiseis 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
Promiseobject 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.See moreNote
If a registered callback is invoked (or would have been invoked if no token was provided) it is guaranteed to be released on the context. This is important if the callback captures a value whose deallocation must occur on a specific thread (such as the main thread). If the callback is not invoked (ignoring tokens) it will be released on whatever thread the promise was resolved on. For example, if a promise is fulfilled, any callback registered with.then(on:token:_:)will be released on the context, but callbacks registered with.catch(on:token:_:)will not. If you need to guarantee the thread that the callback is released on, you should use.always(on:token:_:)or one of the.mapResult(on:token:_:)variants.Declaration
Swift
public struct Promise<Value, Error>extension Promise: Equatable -
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
Promisehas no more references to it this automatically cancels the promise, but a retain cycle prevents this.This is returned from
See morePromise.cancellable.Declaration
Swift
public struct PromiseCancellable -
A
Promiseadapter that automatically applies aPromiseInvalidationToken.This exposes the same methods as
Promisebut it always passes a givenPromiseInvalidationTokento the underlying promise.Important
TokenPromiseautomatically cancels any returned childPromises when thePromiseInvalidationTokenis invalidated. The only exception isPromises returned fromtap()ortap(on:_:), as these methods do not propagate cancellation.A
TokenPromiseis created with the methodPromise.withToken(_:). The wrappedPromisecan be accessed with the.innerproperty.Example
See moremethodReturningPromise() .withToken(promiseToken) .then({ (value) in // handle value }).catch({ (error) in // handle error })Declaration
Swift
public struct TokenPromise<Value, Error>extension TokenPromise: Equatable
View on GitHub
Structures Reference