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

Async/await #1291

Merged
merged 10 commits into from
Jul 25, 2022
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
8 changes: 8 additions & 0 deletions PromiseKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
63CF6D80203CD19200EC8927 /* ThenableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63CF6D7F203CD19200EC8927 /* ThenableTests.swift */; };
63D9B2EF203385FD0075C00B /* race.m in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2EE203385FD0075C00B /* race.m */; };
63D9B2F120338D5D0075C00B /* Deprecations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2F020338D5D0075C00B /* Deprecations.swift */; };
9E4170F9287D88C900A3B4B5 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170F8287D88C800A3B4B5 /* Async.swift */; };
9E4170FC287D8DF900A3B4B5 /* AsyncTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */; };
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E66231526FE5A8C00FA25CB /* RaceTests.m */; };
C013F7382048E3B6006B57B1 /* MockNodeEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */; };
C013F73A2049076A006B57B1 /* JSPromise.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7392049076A006B57B1 /* JSPromise.swift */; };
Expand Down Expand Up @@ -224,6 +226,8 @@
63CF6D7F203CD19200EC8927 /* ThenableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThenableTests.swift; sourceTree = "<group>"; };
63D9B2EE203385FD0075C00B /* race.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = race.m; path = Sources/race.m; sourceTree = "<group>"; };
63D9B2F020338D5D0075C00B /* Deprecations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Deprecations.swift; path = Sources/Deprecations.swift; sourceTree = "<group>"; };
9E4170F8287D88C800A3B4B5 /* Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Async.swift; path = Sources/Async.swift; sourceTree = "<group>"; };
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncTests.swift; sourceTree = "<group>"; };
9E66231526FE5A8C00FA25CB /* RaceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RaceTests.m; sourceTree = "<group>"; };
C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockNodeEnvironment.swift; path = "Tests/JS-A+/MockNodeEnvironment.swift"; sourceTree = "<group>"; };
C013F7392049076A006B57B1 /* JSPromise.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSPromise.swift; path = "Tests/JS-A+/JSPromise.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -336,6 +340,7 @@
6317518A1D59765700A9DDDC /* Core */ = {
isa = PBXGroup;
children = (
9E4170FA287D8DBD00A3B4B5 /* AsyncTests.swift */,
63CF6D7F203CD19200EC8927 /* ThenableTests.swift */,
632FBBE41F33B338008F8FBB /* CatchableTests.swift */,
635D64081D59635300BC0AF5 /* PromiseTests.swift */,
Expand Down Expand Up @@ -434,6 +439,7 @@
63B0AC601D595E4C00FA21D9 /* Sources.swift */ = {
isa = PBXGroup;
children = (
9E4170F8287D88C800A3B4B5 /* Async.swift */,
636A29191F1C156B001229C2 /* Promise.swift */,
636A29221F1C17A6001229C2 /* Guarantee.swift */,
636A29201F1C1716001229C2 /* Thenable.swift */,
Expand Down Expand Up @@ -689,6 +695,7 @@
63CF6D7C203CCDAB00EC8927 /* GuaranteeTests.swift in Sources */,
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */,
639BF757203DF03100FA577B /* Utilities.swift in Sources */,
9E4170FC287D8DF900A3B4B5 /* AsyncTests.swift in Sources */,
635D64261D59635300BC0AF5 /* WhenResolvedTests.swift in Sources */,
635D64231D59635300BC0AF5 /* AfterTests.swift in Sources */,
63CF6D7A203CC66000EC8927 /* ErrorTests.swift in Sources */,
Expand Down Expand Up @@ -753,6 +760,7 @@
63B0AC801D595E6300FA21D9 /* after.swift in Sources */,
63B18AEC1F2D205C00B79E37 /* CustomStringConvertible.swift in Sources */,
085B96BF21A9B37C00E5E22F /* LogEvent.swift in Sources */,
9E4170F9287D88C900A3B4B5 /* Async.swift in Sources */,
6330B5E11F2E991200D60528 /* Configuration.swift in Sources */,
63B912AA1F1D7B1300D49110 /* firstly.swift in Sources */,
636A29211F1C1716001229C2 /* Thenable.swift in Sources */,
Expand Down
26 changes: 26 additions & 0 deletions Sources/Async.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if swift(>=5.5)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Guarantee {
func async() async -> T {
await withCheckedContinuation { continuation in
done { value in
continuation.resume(returning: value)
}
}
}
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Promise {
func async() async throws -> T {
try await withCheckedThrowingContinuation { continuation in
done { value in
continuation.resume(returning: value)
}.catch { error in
continuation.resume(throwing: error)
}
}
}
}
#endif

81 changes: 81 additions & 0 deletions Tests/CorePromise/AsyncTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import PromiseKit
import Dispatch
import XCTest

private enum Error: Swift.Error { case dummy }

class AsyncTests: XCTestCase {

#if swift(>=5.5)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testAsyncPromiseValue() async throws {
let promise = after(.milliseconds(100)).then(on: nil){ Promise.value(1) }
let value = try await promise.async()
XCTAssertEqual(value, 1)
}

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

}
#else
func testAsyncPromiseValue() {

}
#endif

#if swift(>=5.5)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testAsyncGuaranteeValue() async {
let guarantee = after(.milliseconds(100)).then(on: nil){ Guarantee.value(1) }
let value = await guarantee.async()
XCTAssertEqual(value, 1)
}

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

}
#else
func testAsyncGuaranteeValue() {

}
#endif

#if swift(>=5.5)
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testAsyncPromiseThrow() async throws {
do {
let promise = after(.milliseconds(100)).then(on: nil){ Promise(error: Error.dummy) }.then(on: nil){ Promise.value(1) }
try await _ = promise.async()
XCTAssert(false)
} catch {
switch error as? Error {
case .dummy:
XCTAssert(true)
default:
XCTAssert(false)
}
}
}

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

}
#else
func testAsyncPromiseThrow() {

}
#endif
}

15 changes: 15 additions & 0 deletions Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ extension AfterTests {
]
}

extension AsyncTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__AsyncTests = [
("testAsyncGuaranteeValue", testAsyncGuaranteeValue),
("testAsyncGuaranteeValue", testAsyncGuaranteeValue),
("testAsyncPromiseThrow", testAsyncPromiseThrow),
("testAsyncPromiseThrow", testAsyncPromiseThrow),
("testAsyncPromiseValue", testAsyncPromiseValue),
("testAsyncPromiseValue", testAsyncPromiseValue),
]
}

extension CancellationTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -289,6 +303,7 @@ extension ZalgoTests {
public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(AfterTests.__allTests__AfterTests),
testCase(AsyncTests.__allTests__AsyncTests),
testCase(CancellationTests.__allTests__CancellationTests),
testCase(CatchableTests.__allTests__CatchableTests),
testCase(GuaranteeTests.__allTests__GuaranteeTests),
Expand Down