CoFuture
public class CoFuture<Value>
extension CoFuture: Hashable
extension CoFuture: Cancellable
Holder for a result that will be provided later.
CoFuture
and its subclass CoPromise
are the implementation of the Future/Promise approach.
They allow to launch asynchronous tasks and immediately returnCoFuture
with its future results.
The available result can be observed by the whenComplete()
callback
or by await()
inside a coroutine without blocking a thread.
func makeFutureOne(args) -> CoFuture<Response> {
let promise = CoPromise<Response>()
someAsyncFuncWithCallback { response in
. . . do some work . . .
promise.success(response)
}
return promise
}
func makeFutureTwo(args) -> CoFuture<Response> {
queue.coroutineFuture {
let future = makeFutureOne(args)
. . . do some work . . .
let response = try future.await()
. . . create result using response . . .
return result
}
}
func performSomeWork(args) {
let future = makeFutureTwo(args)
mainQueue.startCoroutine {
. . . do some work . . .
let result = try future.await()
. . . do some work using result . . .
}
}
-
Initializes a future with result.
Declaration
Swift
@inlinable public convenience init(result: Result<Value, Error>)
Parameters
result
The result provided by this future.
-
Initializes a future with success value.
Declaration
Swift
@inlinable public convenience init(value: Value)
Parameters
value
The value provided by this future.
-
Initializes a future with error.
Declaration
Swift
@inlinable public convenience init(error: Error)
Parameters
error
The error provided by this future.
-
Returns completed result or nil if this future has not been completed yet.
Declaration
Swift
public var result: Result<Value, Error>? { get }
-
Returns
true
when the current future is canceled.Declaration
Swift
@inlinable public var isCanceled: Bool { get }
-
Cancels the current future.
Declaration
Swift
@inlinable public func cancel()
-
Adds an observer callback that is called when the
CoFuture
has any result.Declaration
Swift
public func whenComplete(_ callback: @escaping (Result<Value, Error>) -> Void)
Parameters
callback
The callback that is called when the
CoFuture
is fulfilled. -
Adds an observer callback that is called when the
CoFuture
has a success result.Declaration
Swift
@inlinable public func whenSuccess(_ callback: @escaping (Value) -> Void)
Parameters
callback
The callback that is called with the successful result of the
CoFuture
. -
Adds an observer callback that is called when the
CoFuture
has a failure result.Declaration
Swift
@inlinable public func whenFailure(_ callback: @escaping (Error) -> Void)
Parameters
callback
The callback that is called with the failed result of the
CoFuture
. -
Adds an observer callback that is called when the
CoFuture
is canceled.Declaration
Swift
@inlinable public func whenCanceled(_ callback: @escaping () -> Void)
Parameters
callback
The callback that is called when the
CoFuture
is canceled.
-
Await for the result of this
CoFuture
without blocking the current thread. Must be called inside a coroutine.//execute someSyncFunc() on global queue and return its future result let future = DispatchQueue.global().coroutineFuture { someSyncFunc() } //start coroutine on main thread DispatchQueue.main.startCoroutine { //await result of future let result = try future.await() }
Throws
The failed result of theCoFuture
.Declaration
Swift
public func await() throws -> Value
Return Value
The value of the
CoFuture
when it is completed.
-
Declaration
Swift
@inlinable public static func == (lhs: CoFuture, rhs: CoFuture) -> Bool
-
Declaration
Swift
@inlinable public func hash(into hasher: inout Hasher)
-
Returns a publisher that emits result of this
CoFuture
.Declaration
Swift
public func publisher() -> AnyPublisher<Value, Error>