Swift Academy · Lesson

Task and Task Cancellation

Creating structured tasks, checking cancellation and cooperative cancellation.

Lesson 2 of 412 steps

Task and Task Cancellation is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

`Task` is the fundamental unit of concurrency in Swift. It runs async work and supports structured cancellation for clean resource management.

Creating a Task

```swift Task { let user = try await fetchUser(id: 1) print(user.name) } ``` `Task` creates a new concurrent execution context.

Task Priority

```swift Task(priority: .userInitiated) { await loadContent() } Task(priority: .background) { await prefetch() } Task(priority: .utility) { await syncData() } ```

Task.cancel()

```swift var task: Task? func startLoading() { task = Task { await heavyWork() } } func stopLoading() { task?.cancel() } ```

Checking Cancellation

```swift func process() async throws { for item in items { try Task.checkCancellation() // throws if cancelled process(item) } } ```

Task.isCancelled

```swift func stream() async { while !Task.isCancelled { let data = await fetch() emit(data) } } ```

Detached Tasks

```swift Task.detached(priority: .background) { // NOT inheriting actor context or priority of the caller await heavyBackgroundWork() } ```

withTaskCancellationHandler

```swift await withTaskCancellationHandler { await doWork() } onCancel: { cleanup() } ```

Task Return Values

```swift let task = Task { 6 * 7 } let result = await task.value // 42 ```

Sleep

```swift try await Task.sleep(for: .seconds(2)) // Swift 5.7+ try await Task.sleep(nanoseconds: 1_000_000_000) // 1 second ```

Quick Check

Which method throws a `CancellationError` if the task has been cancelled?

Recap

Key takeaways: • `Task { }` — creates async work unit • `task.cancel()` + `Task.checkCancellation()` — cooperative cancellation • `Task.isCancelled` — poll-style check • `Task.detached` — no actor/priority inheritance • `Task.sleep(for:)` — non-blocking delay Next: async let.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Task and Task Cancellation” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Task and Task Cancellation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Task and Task Cancellation”?

Creating structured tasks, checking cancellation and cooperative cancellation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Task and Task Cancellation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. async/await Syntax and Calling Async Functions
  2. Task and Task Cancellation
  3. async let and Concurrent Child Tasks
  4. Actors: Protecting Shared Mutable State
← Back to Swift Academy