CoFuture
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 . . .
    }
}
For coroutine error handling you can use standart do-catch statement or use CoFuture as an alternative.
//execute coroutine and return CoFuture<Void> that we will use for error handling
DispatchQueue.main.coroutineFuture {
    let result = try makeSomeFuture().await()
    . . . use result . . .
}.whenFailure { error in
    . . . handle error . . .
}
Apple has introduced a new reactive programming framework Combine
that makes writing asynchronous code easier and includes a lot of convenient and common functionality.
We can use it with coroutines by making CoFuture a subscriber and await its result.
//create Combine publisher
let publisher = URLSession.shared.dataTaskPublisher(for: url).map(\.data)
//execute coroutine on the main thread
DispatchQueue.main.startCoroutine {
    //subscribe CoFuture to publisher
    let future = publisher.subscribeCoFuture()
    //await data without blocking the thread
    let data: Data = try future.await()
}
- 
                  
                  Initializes a future that invokes a promise closure. func someAsyncFunc(callback: @escaping (Result<Int, Error>) -> Void) { ... } let future = CoFuture(promise: someAsyncFunc)DeclarationSwift @inlinable public convenience init(promise: (@escaping (Result<Value, Error>) -> Void) -> Void)ParameterspromiseA closure to fulfill this future. 
- 
                  
                  Starts a new coroutine and initializes future with its result. Note If you cancel thisCoFuture, it will also cancel the coroutine that was started inside of it.func sum(future1: CoFuture<Int>, future2: CoFuture<Int>) -> CoFuture<Int> { CoFuture { try future1.await() + future2.await() } }DeclarationSwift @inlinable public convenience init(task: @escaping () throws -> Value)ParameterstaskThe closure that will be executed inside the coroutine. 
- 
                  
                  Initializes a future with result. DeclarationSwift @inlinable public convenience init(result: Result<Value, Error>)ParametersresultThe result provided by this future. 
- 
                  
                  Returns completed result or nil if this future has not been completed yet. DeclarationSwift public var result: Result<Value, Error>? { get }
- 
                  
                  Returns truewhen the current future is canceled.DeclarationSwift @inlinable public var isCanceled: Bool { get }
- 
                  
                  Cancels the current future. DeclarationSwift public func cancel()
- 
                  
                  Await for the result of this CoFuturewithout 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.DeclarationSwift @inlinable public func await() throws -> ValueReturn ValueThe value of the CoFuturewhen it is completed.
- 
                  
                  Await for the result of this CoFuturewithout blocking the current thread. Must be called inside a coroutine.Throws The failed result of theCoFuture.DeclarationSwift public func await(timeout: DispatchTimeInterval) throws -> ValueParameterstimeoutThe time interval to await for a result. Return ValueThe value of the CoFuturewhen it is completed.
- 
                  
                  When future is fulfilled, run the provided callback, which performs a synchronous computation and return transformed value. DeclarationSwift @inlinable public func map<NewValue>(_ transform: @escaping (Value) throws -> NewValue) -> CoFuture<NewValue>ParameterstransformFunction that will receive the value and return a new transformed value or throw an error. Return ValueA future that will receive the eventual value. 
- 
                  
                  When future is in an error state, run the provided callback, which can recover from the error and return a new value. DeclarationSwift @inlinable public func recover(_ transform: @escaping (Error) throws -> Value) -> CoFutureParameterstransformFunction that will receive the error and return a new value or throw an error. Return ValueA future that will receive the recovered value. 
- 
                  
                  When future is fulfilled, run the provided callback, which performs a synchronous computation and return transformed result. DeclarationSwift public func mapResult<NewValue>(_ transform: @escaping (Result<Value, Error>) -> Result<NewValue, Error>) -> CoFuture<NewValue>ParameterstransformFunction that will receive the result and return a new transformed result. Return ValueA future that will receive the eventual result. 
- 
                  
                  When the current CoFutureis fulfilled, run the provided callback, which will provide a newCoFuture.This allows you to dynamically dispatch new asynchronous tasks as phases in a longer series of processing steps. Note that you can use the results of the current CoFuturewhen determining how to dispatch the next operation.DeclarationSwift @inlinable public func flatMap<NewValue>(_ callback: @escaping (Value) -> CoFuture<NewValue>) -> CoFuture<NewValue>ParameterscallbackFunction that will receive the value and return a new CoFuture.Return ValueA future that will receive the eventual value. 
- 
                  
                  When the current CoFutureis in an error state, run the provided callback, which may recover from the error by returning aCoFuture.DeclarationSwift @inlinable public func flatMapError(_ callback: @escaping (Error) -> CoFuture) -> CoFutureParameterscallbackFunction that will receive the error value and return a new value lifted into a new CoFuture.Return ValueA future that will receive the recovered value. 
- 
                  
                  When the current CoFutureis fulfilled, run the provided callback, which will provide a newCoFuture.DeclarationSwift public func flatMapResult<NewValue>(_ callback: @escaping (Result<Value, Error>) -> CoFuture<NewValue>) -> CoFuture<NewValue>ParameterscallbackFunction that will receive the result and return a new CoFuture.Return ValueA future that will receive the eventual value. 
- 
                  
                  Adds an observer callback that is called when the CoFuturehas any result.DeclarationSwift @inlinable public func whenComplete(_ callback: @escaping (Result<Value, Error>) -> Void)ParameterscallbackThe callback that is called when the CoFutureis fulfilled.
- 
                  
                  Adds an observer callback that is called when the CoFuturehas a success result.DeclarationSwift @inlinable public func whenSuccess(_ callback: @escaping (Value) -> Void)ParameterscallbackThe callback that is called with the successful result of the CoFuture.
- 
                  
                  Adds an observer callback that is called when the CoFuturehas a failure result.DeclarationSwift @inlinable public func whenFailure(_ callback: @escaping (Error) -> Void)ParameterscallbackThe callback that is called with the failed result of the CoFuture.
- 
                  
                  Adds an observer callback that is called when the CoFutureis canceled.DeclarationSwift @inlinable public func whenCanceled(_ callback: @escaping () -> Void)ParameterscallbackThe callback that is called when the CoFutureis canceled.
- 
                  
                  Adds an observer callback that is called when the CoFuturehas any result.DeclarationSwift @inlinable public func whenComplete(_ callback: @escaping () -> Void)ParameterscallbackThe callback that is called when the CoFutureis fulfilled.
- 
                  
                  DeclarationSwift @inlinable public static func == (lhs: CoFuture, rhs: CoFuture) -> Bool
- 
                  
                  DeclarationSwift @inlinable public func hash(into hasher: inout Hasher)
- 
                  
                  Returns a publisher that emits result of this CoFuture.DeclarationSwift public func publisher() -> AnyPublisher<Value, Error>
 CoFuture Class Reference
        CoFuture Class Reference