Skip to content

Commit a84226c

Browse files
authored
Merge pull request #1321 from RomanPodymov/master
func when(fulfilled thenables: [any Thenable])
2 parents 143091e + 6008964 commit a84226c

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Sources/when.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public func when<U: Thenable>(fulfilled thenables: [U]) -> Promise<[U.T]> {
104104
return _when(thenables).map(on: nil) { thenables.map{ $0.value! } }
105105
}
106106

107+
#if swift(>=5.7)
108+
public func when(fulfilled thenables: [any Thenable]) -> Promise<[Any]> {
109+
return _when(thenables.map { $0.asVoid()}).map(on: nil) { thenables.map { $0.value! } }
110+
}
111+
#endif
112+
107113
/// Wait for all promises in a set to fulfill.
108114
public func when<U: Thenable>(fulfilled promises: U...) -> Promise<Void> where U.T == Void {
109115
return _when(promises)

Tests/CorePromise/WhenTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ class WhenTests: XCTestCase {
3737
waitForExpectations(timeout: 1, handler: nil)
3838
}
3939

40+
func testAnyInt() {
41+
#if swift(>=5.7)
42+
let e1 = expectation(description: "")
43+
let p1 = Promise.value(1)
44+
let g2 = Guarantee.value(2)
45+
let p3 = Promise.value(3)
46+
let g4 = Guarantee.value(4)
47+
48+
when(fulfilled: [p1, g2, p3, g4]).done { x in
49+
XCTAssertEqual(x[0] as? Int, 1)
50+
XCTAssertEqual(x[1] as? Int, 2)
51+
XCTAssertEqual(x[2] as? Int, 3)
52+
XCTAssertEqual(x[3] as? Int, 4)
53+
XCTAssertEqual(x.count, 4)
54+
e1.fulfill()
55+
}.silenceWarning()
56+
waitForExpectations(timeout: 1, handler: nil)
57+
#endif
58+
}
59+
4060
func testDoubleTuple() {
4161
let e1 = expectation(description: "")
4262
let p1 = Promise.value(1)

Tests/CorePromise/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ extension WhenTests {
267267
// to regenerate.
268268
static let __allTests__WhenTests = [
269269
("testAllSealedRejectedFirstOneRejects", testAllSealedRejectedFirstOneRejects),
270+
("testAnyInt", testAnyInt),
270271
("testDoubleTuple", testDoubleTuple),
271272
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
272273
("testEmpty", testEmpty),

0 commit comments

Comments
 (0)