[Concurrency] Continuations for interfacing async tasks with synchronous code

I think what you mean is that the value of OperationResult passed to *Continuation.resume(returning:) could be inspected, and if that value passes some test the task should be considered cancelled? That’s not the scenario I’m referring to — rather I’m referring to a scenario where the task itself is cancelled first, and that cancellation needs to be propagated to the wrapped callback-based API lest it continue doing work that’s no longer required.

Take for example this usage of withUnsafeThrowingContinuation wrapping URLSession.dataTask:

func download(url: URL) async throws -> Data? {
	return try await withUnsafeThrowingContinuation { continuation in
		let urlSessionTask = URLSession.shared.dataTask(with: url) { data, _, error in
			// EDIT: Lantua notes below that this next line shouldn’t be here
			//guard !continuation.isTaskCancelled() else { return }
			if let error = error {
				continuation.resume(throwing: error)
			} else {
				continuation.resume(returning: data)
			}
		}
		urlSessionTask.resume()

		continuation.taskCancellationHandler = {
			urlSessionTask.cancel()
		}
	}
}

Here’s some context to further clarify. IIUC when one of two concurrent child tasks fails, the other gets cancelled. I.e. in the example below when the “bad” download’s task fails and throws an error, the “good” download’s task gets cancelled. That cancellation should propagate and eventually call URLSessionTask.cancel to avoid unnecessarily downloading more of the “good” data.

func main() async -> Void {
    async let goodDownloadData = download(url: URL(string: "https://example.com/good-data")!)
    async let badDownloadData = download(url: URL(string: "https://httpbin.org/status/404")!)
    do { print(try await [goodDownloadData, badDownloadData]) }
    catch { print("The “bad” download failed:", error) }
}