Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import Foundation
import Combine
struct DataTaskSubscription: Subscription {
let task: URLSessionTask
let combineIdentifier: CombineIdentifier
func request(_ demand: Subscribers.Demand) {
func cancel() {
struct DataTaskPublisher: Publisher {
let session: URLSession
let request: URLRequest
public typealias Output = (data: Data, response: URLResponse)
public typealias Failure = URLError
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
assert(error is URLError)
subscriber.receive(completion: .failure(error as! URLError))
_ = subscriber.receive((data!, response!))
subscriber.receive(completion: .finished)
let subscription = DataTaskSubscription(task: task, combineIdentifier: CombineIdentifier())
subscriber.receive(subscription: subscription)
extension URLSession {
func dataTaskPublisher(for request: URLRequest) -> DataTaskPublisher {
DataTaskPublisher(session: self, request: request)
Copy link

niazoff commented Jun 13, 2019


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment