Classes

The following classes are available globally.

  • 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 . . .
        }
    }
    
    See more

    Declaration

    Swift

    public class CoFuture<Value>
    extension CoFuture: Hashable
    extension CoFuture: Cancellable
  • A promise to provide a result later.

    CoPromise is a subclass of CoFuture that allows to deliver the result. You can set the result to CoPromise only once, other attempts will be ignored.

    See more

    Declaration

    Swift

    public final class CoPromise<Value> : CoFuture<Value>