Skip to content

byoth/RxAsyncAwait

Repository files navigation

RxAsyncAwait

SwiftPM Version Version Hits

RxAsyncAwait is an extension for using asynchronous functions on streams in RxSwift.

I don't think it's preferred, but if you need to use asynchronous functions in the RxSwift streams, you'll have to write some annoying code to handle it because RxSwift operators don't support easy use for asynchronous functions.

This library can keep your code simple even if that happens.

Usage

TryAwait

Observable.just("https://github.com/byoth")
    .compactMap { URL(string: $0) }
    .tryAwait { try await URLSession.shared.data(from: $0) }
    .subscribe()
    .disposed(by: disposeBag)

Await

Observable.just("https://github.com/byoth")
    .compactMap { URL(string: $0) }
    .await { await getData(from: $0) }
    .subscribe()
    .disposed(by: disposeBag)

func getData(from url: URL) async -> (Data, URLResponse)? {
    try? await URLSession.shared.data(from: url)
}

Installation

Swift Package Manager

import PackageDescription

let package = Package(
    name: "Your App",
    dependencies: [
        .package(url: "https://github.com/byoth/RxAsyncAwait.git", .upToNextMajor(from: "0.1.0")),
    ]
)

CocoaPods

target 'Your Target' do
    pod 'RxAsyncAwait', '~> 0.1'
end

Requirements

  • Xcode 13.2
  • Swift 5.5

Dependencies

About

RxSwift extensions for using asynchronous functions on streams

Resources

License

Stars

Watchers

Forks

Packages

No packages published