-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1291 from RomanPodymov/v6
Async/await
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters