URLSession in Combine provides a dataTaskPublisher which handles your network request as a publisher in the pipeline. As soon as the subscription is created the request kicks off asynchronously, meaning the rest of the code continues executing immediately which is why "Subscription stored" prints before the response even comes back. Once the response arrives it flows downstream through the operators — map(\.data) extracts just the raw data from the response, then decode converts that data into your model. The value then hits the sink where you handle it, and finally once everything is done the pipeline sends a completion event downstream through each operator until it reaches the sink's receiveCompletion block. The order of receiveValue and receiveCompletion parameters in the sink doesn't affect execution — Swift uses named parameters so it always knows which is which, and values always arrive before completion regardless of how you write it in code.
Combine provides built-in support for networking using:
URLSession.DataTaskPublisher
This allows network requests to be treated as asynchronous data streams.
Instead of using callbacks, Combine enables:
request → response → transformation → result
All handled inside a single reactive pipeline.
A network request is created using:
URLSession.shared.dataTaskPublisher(for: url)
This publisher emits:
(data: Data, response: URLResponse)
It represents:
raw response data
HTTP response metadata
To decode JSON, we define a model:
struct Post: Decodable {
let id: Int
let title: String
}