Skip to content

Instantly share code, notes, and snippets.

@xbladesub
Forked from Monntay/asyncAwaitInCombine.swift
Created November 21, 2021 07:37
Show Gist options
  • Save xbladesub/a9e3c81693bf0f1da7247b5ead553891 to your computer and use it in GitHub Desktop.
Save xbladesub/a9e3c81693bf0f1da7247b5ead553891 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