Skip to content

Instantly share code, notes, and snippets.

@lipka
Last active August 13, 2023 15:10
Show Gist options
  • Save lipka/599e6bd793dfb22a9ff44d33fc7cae13 to your computer and use it in GitHub Desktop.
Save lipka/599e6bd793dfb22a9ff44d33fc7cae13 to your computer and use it in GitHub Desktop.
Force error handling in for Swift's Task when using structured concurrency.
// Swift's `Task` silently discards errors from throwable callsites. `SafeTask` forces you to explicitly handle errors at the callsite and helps you avoid mistakes with silently discarded errors.
@discardableResult func SafeTask<Success>(priority: TaskPriority? = nil, operation: @escaping () async -> Success) -> Task<Success, Never> where Success: Sendable {
return Task(priority: priority, operation: {
await operation()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment