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 aFuture<Value>
, you can use the global functionspromise()
, or create a newPromise<Value>
to fulfill in an asynchronous fashion. To create a new promise, returning aFuture<Value>
, follow this pattern:promise { "Hello World!" }
The above function will return a
Future<String>
eventually carrying the stirngHello 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 itsFuture
:
See morefunc someAsynOperation(args) -> Future<ResultType> { let promise = Promise<ResultType>() dispatchQueue.async { ... if success ... promise.fulfill(value) ... if error ... promise.reject(error) } return promise.future }
Declaration
Swift
public struct Promise<Value>
-
Declaration
Swift
public struct Promises<Source>