Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resume continuation on cancellation when using .async() #1334

Merged
merged 2 commits into from
May 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion PromiseKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "PromiseKit"

s.version = '8.1.1'
s.version = '8.1.2'

s.source = {
:git => "https://github.com/mxcl/#{s.name}.git",
Expand Down
4 changes: 2 additions & 2 deletions PromiseKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 8.1.1;
CURRENT_PROJECT_VERSION = 8.1.2;
DEBUG_INFORMATION_FORMAT = dwarf;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down Expand Up @@ -1008,7 +1008,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 8.1.1;
CURRENT_PROJECT_VERSION = 8.1.2;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public extension Promise {
try await withCheckedThrowingContinuation { continuation in
done { value in
continuation.resume(returning: value)
}.catch { error in
}.catch(policy: .allErrors) { error in
continuation.resume(throwing: error)
}
}
Expand Down
50 changes: 50 additions & 0 deletions Tests/CorePromise/AsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,55 @@ class AsyncTests: XCTestCase {

}
#endif

#if swift(>=5.5)
#if canImport(_Concurrency)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testAsyncPromiseCancel() async throws {
do {
let p = after(seconds: 0).done { _ in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

let p = after(seconds: 0).then { _ in
    throw LocalError.cancel
}.done {
    XCTFail()
}.catch { _ in
    XCTFail()
}

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChaosCoder OK, I will fix this, thanks for the pull request.

throw LocalError.cancel
}.done {
XCTFail()
}
p.catch { _ in
XCTFail()
}
try await p.async()
XCTAssert(false)
} catch {
guard let cancellableError = error as? CancellableError else { return XCTFail("Unexpected error type") }
XCTAssertTrue(cancellableError.isCancelled)
}
}

@available(iOS, deprecated: 13.0)
@available(macOS, deprecated: 10.15)
@available(tvOS, deprecated: 13.0)
@available(watchOS, deprecated: 6.0)
func testAsyncPromiseCancel() {

}
#else
func testAsyncPromiseCancel() {

}
#endif
#else
func testAsyncPromiseCancel() {

}
#endif
}

private enum LocalError: CancellableError {
case notCancel
case cancel

var isCancelled: Bool {
switch self {
case .notCancel: return false
case .cancel: return true
}
}
}