Last active
August 13, 2023 15:10
-
-
Save lipka/599e6bd793dfb22a9ff44d33fc7cae13 to your computer and use it in GitHub Desktop.
Force error handling in for Swift's Task when using structured concurrency.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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