Swiftpack.co -  petrpavlik/CombineToAsyncAwait as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
petrpavlik/CombineToAsyncAwait
Automatically convert Combine Publishers into Swift 5.5 async methods.
.package(url: "https://github.com/petrpavlik/CombineToAsyncAwait.git", from: "0.1.0")

CombineToAsyncAwait

Automatically convert Combine Publishers into Swift 5.5 async methods.

Example

Let's convert a Combine publisher provided by Apple to make a network request into async/await

Combine implementation

URLSession.shared.dataTaskPublisher(for: url).sink { completion in
    // check error
} receiveValue: { value in
    // process value
}.store(in: &cancellables)

Async/await bridge

import CombineToAsyncAwait

extension URLSession {
    func data(for url: URL) async throws -> (Data, URLResponse) {
        try await self.dataTaskPublisher(for: url).asyncValue()
    }
}

final class NetworkingTests: XCTestCase {
    func testNetworkRequest() async {
        do {
            let (data, response) = try await URLSession.shared.data(for: URL(string: "https://google.com")!)
        } catch {

        }
    }
}

Note: Apple has actually introduced async/await versions of this as a part of foundation on iOS 15 and corresponding OSes, so this is only to demonstrate how easy it is to turn a Combine publisher into an async method.


Contributions and more features, such as support for async sequences will be added.

GitHub

link
Stars: 1
Last commit: 5 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Initial release
5 days ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API