Created
July 7, 2022 20:00
-
-
Save circAssimilate/004b2aa254de8c7ee305de3f135ca8d3 to your computer and use it in GitHub Desktop.
Playing with Swift task cancelation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: [Previous](@previous) | |
import Foundation | |
import AppKit | |
Task { | |
print("[DEBUG] BEING") | |
var task: Task<String, Error>? | |
task = Task { | |
print("[DEBUG] Start Task") | |
try await Task.sleep(nanoseconds: 3_000_000_000) | |
print("[DEBUG] End Task") | |
return "success" | |
} | |
try await Task.sleep(nanoseconds: 1_500_000_000) | |
print("[DEBUG] CANCELING") | |
task?.cancel() | |
do { | |
let result = try await task?.value | |
print("Result: \(String(describing: result))") | |
} catch { | |
print("Failure") | |
} | |
print("[DEBUG] END") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment