Skip to content

Instantly share code, notes, and snippets.

@circAssimilate
Created July 7, 2022 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save circAssimilate/004b2aa254de8c7ee305de3f135ca8d3 to your computer and use it in GitHub Desktop.
Save circAssimilate/004b2aa254de8c7ee305de3f135ca8d3 to your computer and use it in GitHub Desktop.
Playing with Swift task cancelation.
//: [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