Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Then/Then.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ extension Then where Self: Any {
try block(self)
}

#if swift(>=5.5) && canImport(_Concurrency)
/// Async-friendly version of `.do` that accepts an async-throws closure.
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@inlinable
public func `do`(_ block: (Self) async throws -> Void) async rethrows {
try await block(self)
}
#endif

}

extension Then where Self: AnyObject {
Expand Down
17 changes: 16 additions & 1 deletion Tests/ThenTests/ThenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ struct User {
}
extension User: Then {}

class Timer {
func run() async -> Bool {
try? await Task.sleep(nanoseconds: 1_000_000_000)
return true
}
}
extension Timer: Then {}

class ThenTests: XCTestCase {

func testThen_NSObject() {
Expand Down Expand Up @@ -63,12 +71,19 @@ class ThenTests: XCTestCase {
XCTAssertEqual(UserDefaults.standard.string(forKey: "username"), "devxoul")
}

func testAsyncDo() async {
let t = Timer()
await t.do {
let obj = await $0.run()
XCTAssertTrue(obj)
}
}

func testRethrows() {
XCTAssertThrowsError(
try NSObject().do { _ in
throw NSError(domain: "", code: 0)
}
)
}

}