Hello everyone,
I am currently going through a course on async await and I think I'm lost on how to handle errors thrown inside a Task. It seems the general sentiment is that errors should be handled inside the context of the task. But what if that's not possible? E.g.
func myFunction() throws {
Task {
try await someFunctionThatMayFail
}
}
If I then call myFunction as:
do {
try myFunction()
} catch {
print("Oh no, I got an error: \(error)")
}
even if there's an error thrown, it will never trigger the catch.
I have seen suggestions for extensions: swift - Throwing erros inside a root Task - Stack Overflow Is this a correct and viable approach?
There also seems to be a suggestion to use the result of the task.
My question is, given a scenario as described in the snippets, is there a correct way of handling this?