Daily iOS
Networking

Canceling an async URLSession request

In this Swift Concurrency code that sends a URLSession request, task.cancel() runs before the network response arrives. What happens?

let task = Task {
    do {
        let (data, _) = try await URLSession.shared.data(from: url)
        print("received \(data.count)")
    } catch {
        print("error: \(error)")
    }
}
task.cancel()

Pick one

Submit