Skip to content

markoengelman/AsyncAwaitWrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages