Forked from Edudjr/migrate-async-await-updated-extension.swift
Last active
January 15, 2025 14:11
-
-
Save comiclandapp/4f1d57a1e48f19673dafc15f2f16683e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum AsyncError: Error { | |
| case finishedWithoutValue | |
| } | |
| extension AnyPublisher { | |
| func async() async throws -> Output { | |
| try await withCheckedThrowingContinuation { continuation in | |
| var cancellable: AnyCancellable? | |
| var finishedWithoutValue = true | |
| cancellable = first() | |
| .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)) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment