/AsyncAwaitWrapper

Simple wrapper that allows us to bridge existing asynchronous code with new Swift concurrency APIs.

Primary LanguageSwiftMIT LicenseMIT

AsyncAwaitWrapper

Code

class AsyncAwaitWrapper<Resource, ResourceError> where ResourceError: Error {
    typealias ResourceLoader = (_ completion: @escaping (Result<Resource, ResourceError>) -> Void) -> Void
    let loader: ResourceLoader
    
    init(loader: @escaping ResourceLoader) {
        self.loader = loader
    }
    
    func load() async -> Result<Resource, ResourceError> {
        return await withCheckedContinuation { continuation in
            loader() { result in
                continuation.resume(returning: result)
            }
        }
    }
    
    func load() async throws -> Resource {
        try await withCheckedThrowingContinuation { continuation in
            loader() { result in
                switch result {
                case let .success(resource):
                    continuation.resume(returning: resource)
                    
                case let .failure(error):
                    continuation.resume(throwing: error)
                }
            }
        }
    }
}

How to use

  • Check provided composer with demonstration how to use.