Yes. Consider this test code:
import Foundation
func main() async {
let t = Task {
do {
print("\(Date.now): task will start")
let url = URL(string: "https://postman-echo.com/delay/10")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
let (data, response) = try await URLSession.shared.data(for: request)
print("\(Date.now): task did complete, response: \(response), data: \(data)")
} catch {
print("\(Date.now): task did error")
}
}
if true {
try? await Task.sleep(for: .seconds(1))
print("\(Date.now): will cancel task")
t.cancel()
print("\(Date.now): did cancel task")
}
print("\(Date.now): waiting")
try? await Task.sleep(for: .seconds(60))
}
await main()
It prints:
2023-05-18 08:18:52 +0000: task will start
2023-05-18 08:18:53 +0000: will cancel task
2023-05-18 08:18:53 +0000: did cancel task
2023-05-18 08:18:53 +0000: waiting
2023-05-18 08:18:53 +0000: task did error
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple