Skip to content

Commit 2189e01

Browse files
authored
When + Parameter Packs (#1359)
1 parent 2bc4439 commit 2189e01

5 files changed

Lines changed: 36 additions & 13 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ concurrency:
1717
jobs:
1818
linux-build:
1919
name: Linux (Swift ${{ matrix.swift }})
20-
runs-on: ubuntu-20.04
20+
runs-on: ubuntu-22.04
2121
strategy:
2222
matrix:
2323
swift:

Sources/when.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ public func when(fulfilled thenables: [any Thenable]) -> Promise<[Any]> {
110110
}
111111
#endif
112112

113+
#if swift(>=5.9)
114+
public func when<each U: Thenable>(fulfilled: repeat each U) -> Promise<(repeat (each U).T)> {
115+
var voidPromises: [Promise<Void>] = []
116+
repeat voidPromises.append((each fulfilled).asVoid())
117+
return _when(voidPromises).map(on: nil) { (repeat (each fulfilled).value!) }
118+
}
119+
#endif
120+
113121
/// Wait for all promises in a set to fulfill.
114122
public func when<U: Thenable>(fulfilled promises: U...) -> Promise<Void> where U.T == Void {
115123
return _when(promises)

Tests/CorePromise/CancellableErrorTests.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ class CancellationTests: XCTestCase {
100100
}
101101

102102
func testDoesntCrashSwift() {
103-
#if os(macOS)
104-
// Previously exposed a bridging crash in Swift
105-
// NOTE nobody was brave enough or diligent enough to report this to Apple :{
106-
// NOTE no Linux test since this constructor doesn’t exist there
107-
XCTAssertFalse(NSError().isCancelled)
108-
#endif
109-
110103
#if canImport(StoreKit)
111104
if #available(watchOS 6.2, *) {
112105
do {

Tests/CorePromise/RaceTests.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,17 @@ class RaceTests: XCTestCase {
7676
func testFulfilledWithNoWinner() {
7777
enum Error: Swift.Error { case test1, test2 }
7878
let ex = expectation(description: "")
79-
let promises: [Promise<Int>] = [after(seconds: 1).map { _ in throw Error.test1 }, after(seconds: 2).map { _ in throw Error.test2 }]
79+
let promises: [Promise<Int>] = [
80+
after(seconds: 1).map { _ in throw Error.test1 },
81+
after(seconds: 2).map { _ in throw Error.test2 }
82+
]
8083
race(fulfilled: promises).done { _ in
81-
XCTFail()
84+
XCTFail("Done with error")
8285
ex.fulfill()
8386
}.catch {
84-
guard let pmkError = $0 as? PMKError else { return XCTFail() }
85-
guard case .noWinner = pmkError else { return XCTFail() }
86-
guard pmkError.debugDescription == "All thenables passed to race(fulfilled:) were rejected" else { return XCTFail() }
87+
guard let pmkError = $0 as? PMKError else { return XCTFail("error is not PMKError") }
88+
guard case .noWinner = pmkError else { return XCTFail("error is not .noWinner") }
89+
guard pmkError.debugDescription == "All thenables passed to race(fulfilled:) were rejected" else { return XCTFail("Not all thenables passed to race(fulfilled:) were rejected") }
8790
ex.fulfill()
8891
}
8992
wait(for: [ex], timeout: 10)

Tests/CorePromise/WhenTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ class WhenTests: XCTestCase {
5757
#endif
5858
}
5959

60+
func testParameterPacks() {
61+
#if swift(>=5.9)
62+
let e1 = expectation(description: "")
63+
let p1 = Promise.value(1)
64+
let g2 = Guarantee.value("Hello")
65+
let p3 = Promise.value("world")
66+
let g4 = Guarantee.value(2)
67+
68+
when(fulfilled: p1, g2, p3, g4).done { x1, x2, x3, x4 in
69+
XCTAssertEqual(x1, 1)
70+
XCTAssertEqual(x2, "Hello")
71+
XCTAssertEqual(x3, "world")
72+
XCTAssertEqual(x4, 2)
73+
e1.fulfill()
74+
}.silenceWarning()
75+
waitForExpectations(timeout: 1, handler: nil)
76+
#endif
77+
}
78+
6079
func testDoubleTuple() {
6180
let e1 = expectation(description: "")
6281
let p1 = Promise.value(1)

0 commit comments

Comments
 (0)