Classes
The following classes are available globally.
-
Used to keep track of observers, in order to supply the ability to remove observers from a future, if the result of a future is no longer interesting.
The following methods return a
FutureObserver:Future<Value>.whenResolved()Future<Value>.whenFulfilled()Future<Value>.whenRejected()
Declaration
Swift
public final class FutureObserver<Value> : AnyFutureObserverextension FutureObserver: Equatable -
Container for a result that will be provided later.
Functions that promise to do work asynchronously return a
Future<Value>. The receipient of a future can observe it to be notified, or to queue more asynchronous work, when the operation completes.A
Future<Value>is regarded as:resolved, when a value is setfulfilled, when a value is set and successfulrejected, when a value is not set, and an error occured
The provider of a
Future<Value>creates a placeholder object before the actual result is available, immeadiately returning the object, providing a dynamic way of structuring complex dependencies for asynchronous work. This is common behavior in Future/Promise implementation across many languages. Referencing these resources may be useful if you’re unfamilliar with the concept of Promises/Futures:The provider of a
Future<Value>may be implemented as follows:func performAsynchronousWork() -> Future<String> { let promise = Promise<String>() DispatchQueue.global().async { ... on success ... promise.fulfill("A string") ... on failure ... promise.reject(error) } return promise.future }When receiving a
Future<Value>, you have a number of options. You can immediately observe the result of the future usingwhenResolved(),whenFulfilled(), orwhenRjected(), or choose to do more asyncrhonous work before, or parallel to observing. EachFuture<Value>can have multiple observers, which are used to both simply return a result, or to queue up other futures.To perform more asynchronous work, once a
Future<Value>is fulfilled, useflatMap(). To transform the fulfilled value of a future into another value, useflatMapThrowing().Options for combining futures into a single future is provided using
and(),fold(), andFuture<Value>.reduce()A
See moreFuture<Value>differs from aPromise<Value>in that a future is the container of a result; the promise being the function that sets that result. This design decision was made, in this library as well as in many others, to prevent receivers ofFuture<Value>to resolve the future themselves.Declaration
View on GitHub
Classes Reference