Complete the code to check if a task has been cancelled.
if Task.isCancelled [1] { print("Task was cancelled") }
The property Task.isCancelled returns true if the task was cancelled. So we check if it is true.
Complete the code to throw a cancellation error inside an async task.
func fetchData() async throws {
guard !Task.[1] else {
throw CancellationError()
}
// continue fetching data
}The property Task.isCancelled is used to check if the current task has been cancelled.
Fix the error in the code to properly handle cancellation inside a Task.
Task {
try await someAsyncFunction()
if [1] {
print("Cancelled")
}
}Inside a Task, use Task.isCancelled to check if the task was cancelled.
Fill both blanks to create a dictionary comprehension that includes only non-cancelled tasks.
let activeTasks = [task: status for (task, status) in allTasks if task.[1] == false && !Task.[2]]
Check each task's isCancelled property and also the static Task.isCancelled to filter out cancelled tasks.
Fill all three blanks to create a dictionary of task names and statuses for tasks that are not cancelled.
let filteredTasks = [[1]: [2] for ([3], status) in tasks if ![3].isCancelled]
The dictionary uses task.name as keys and status as values, iterating over task in tasks.