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

func when(fulfilled thenables: [any Thenable]) #1321

Merged
merged 2 commits into from
Jul 21, 2023
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
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
Loading