Hi Davide!
Thank you so much for the feedback. I also ran into a similar problem when using it for testing. In my case, I had a bug where the publisher never emitted a value neither was completed. This caused the test to run forever. I believe the solution I had was very similar to yours, the only difference is that I had to put a timeout as well. I'd love to update the article with your solution giving you the credits! Do you think this code snippet represents your solution and/or fixes it?
extension AnyPublisher {
func async(timeout: RunLoop.SchedulerTimeType.Stride = .seconds(1)) async throws -> Output {
try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
var finishedWithoutValue = true
cancellable = first()
.timeout(timeout, scheduler: RunLoop.main)
.sink { result in
switch result {
case .finished:
if finishedWithoutValue {
continuation.resume(throwing: AsyncError.finishedWithoutValue)
}
case let .failure(error):
continuation.resume(throwing: error)
}
cancellable?.cancel()
} receiveValue: { value in
finishedWithoutValue = false
continuation.resume(with: .success(value))
}
}
}
}