Skip to content

Instantly share code, notes, and snippets.

@Monntay
Last active March 1, 2022 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Monntay/b5cf7a29383e55283f128b6376947a75 to your computer and use it in GitHub Desktop.
Save Monntay/b5cf7a29383e55283f128b6376947a75 to your computer and use it in GitHub Desktop.
async await in combine
extension Publisher {
func awaitSink(cancellable: inout Set<AnyCancellable>) async throws -> Output {
return try await withCheckedThrowingContinuation { continuation in
self.sink { completion in
switch completion {
case .finished:
break
case .failure(let error):
continuation.resume(with: .failure(error))
}
} receiveValue: { result in
continuation.resume(with: .success(result))
}
.store(in: &cancellable)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment