Skip to content

Commit

Permalink
Merge pull request #1321 from RomanPodymov/master
Browse files Browse the repository at this point in the history
func when(fulfilled thenables: [any Thenable])
  • Loading branch information
mxcl committed Jul 21, 2023
2 parents 143091e + 6008964 commit a84226c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/when.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public func when<U: Thenable>(fulfilled thenables: [U]) -> Promise<[U.T]> {
return _when(thenables).map(on: nil) { thenables.map{ $0.value! } }
}

#if swift(>=5.7)
public func when(fulfilled thenables: [any Thenable]) -> Promise<[Any]> {
return _when(thenables.map { $0.asVoid()}).map(on: nil) { thenables.map { $0.value! } }
}
#endif

/// Wait for all promises in a set to fulfill.
public func when<U: Thenable>(fulfilled promises: U...) -> Promise<Void> where U.T == Void {
return _when(promises)
Expand Down
20 changes: 20 additions & 0 deletions Tests/CorePromise/WhenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ class WhenTests: XCTestCase {
waitForExpectations(timeout: 1, handler: nil)
}

func testAnyInt() {
#if swift(>=5.7)
let e1 = expectation(description: "")
let p1 = Promise.value(1)
let g2 = Guarantee.value(2)
let p3 = Promise.value(3)
let g4 = Guarantee.value(4)

when(fulfilled: [p1, g2, p3, g4]).done { x in
XCTAssertEqual(x[0] as? Int, 1)
XCTAssertEqual(x[1] as? Int, 2)
XCTAssertEqual(x[2] as? Int, 3)
XCTAssertEqual(x[3] as? Int, 4)
XCTAssertEqual(x.count, 4)
e1.fulfill()
}.silenceWarning()
waitForExpectations(timeout: 1, handler: nil)
#endif
}

func testDoubleTuple() {
let e1 = expectation(description: "")
let p1 = Promise.value(1)
Expand Down
1 change: 1 addition & 0 deletions Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ extension WhenTests {
// to regenerate.
static let __allTests__WhenTests = [
("testAllSealedRejectedFirstOneRejects", testAllSealedRejectedFirstOneRejects),
("testAnyInt", testAnyInt),
("testDoubleTuple", testDoubleTuple),
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
("testEmpty", testEmpty),
Expand Down

0 comments on commit a84226c

Please sign in to comment.