Skip to content

Commit

Permalink
Add Future convenience init that supports async closure directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfol committed Jul 16, 2024
1 parent 9bf7229 commit b4fee94
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions Tests/KnitTests/MainActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ private class TestAssembly: Assembly {
let mainClassA = resolver.resolve(MainClassA.self)!

return Future<CustomGlobalActorClass, Never>() { promise in
Task {
let customGlobalActorClass = await CustomGlobalActorClass(
mainClassA: mainClassA
)
promise(.success(customGlobalActorClass))
}
let customGlobalActorClass = await CustomGlobalActorClass(
mainClassA: mainClassA
)
promise(.success(customGlobalActorClass))
}
}
}
Expand All @@ -132,9 +130,7 @@ private class TestAssembly: Assembly {
factory: { resolver in
MainActor.assumeIsolated {
return Future<AsyncInitClass, Never>() { promise in
Task {
promise(.success(await AsyncInitClass()))
}
promise(.success(await AsyncInitClass()))
}
}
}
Expand Down Expand Up @@ -179,3 +175,14 @@ class MainActorTests: XCTestCase {
waitForExpectations(timeout: 5)
}
}

/// Allow `Future`s to be instantiated directly with async closures.
private extension Future {
convenience init(async asyncClosure: @escaping (@escaping Future<Output, Failure>.Promise) async -> Void) {
self.init { promise in
Task {
await asyncClosure(promise)
}
}
}
}

0 comments on commit b4fee94

Please sign in to comment.