Structures

The following structures are available globally.

  • A promise to provide a result later.

    This is the provider API for Future<Value>. If you want to return a Future<Value>, you can use the global functions promise(), or create a new Promise<Value> to fulfill in an asynchronous fashion. To create a new promise, returning a Future<Value>, follow this pattern:

    promise {
        "Hello World!"
    }
    

    The above function will return a Future<String> eventually carrying the stirng Hello World!.

    You can also provide a future using a callback, like so:

    promise(String.self) { completion in
        ... if success ...
        completion(.fulfilled("Hello World!"))
        ... if error ...
        completion(.rejected(error))
    }
    
    

    If you want to provide a Future<Value> in a completely custom manner, you can create a pending promise, resolve it when convenient, and flatMap return its Future:

    func someAsynOperation(args) -> Future<ResultType> {
        let promise = Promise<ResultType>()
        dispatchQueue.async {
            ... if success ...
            promise.fulfill(value)
            ... if error ...
            promise.reject(error)
        }
        return promise.future
    }
    

    Note

    Future is the observable value, while Promise is the function that sets it.
    See more

    Declaration

    Swift

    public struct Promise<Value>
  • A typed container in which to provide specialized futures.

    See PromisesExtended.

    See more

    Declaration

    Swift

    public struct Promises<Source>