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()
    See more

    Declaration

    Swift

    public final class FutureObserver<Value> : AnyFutureObserver
    extension 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 set
    • fulfilled, when a value is set and successful
    • rejected, 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 using whenResolved(), whenFulfilled(), or whenRjected(), or choose to do more asyncrhonous work before, or parallel to observing. Each Future<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, use flatMap(). To transform the fulfilled value of a future into another value, use flatMapThrowing().

    Options for combining futures into a single future is provided using and(), fold(), and Future<Value>.reduce()

    A Future<Value> differs from a Promise<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 of Future<Value> to resolve the future themselves.

    See more

    Declaration

    Swift

    public final class Future<Value> : AnyFuture
    extension Future: Equatable