Skip to content

Instantly share code, notes, and snippets.

@wilg
Last active November 9, 2021 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilg/c13c6b1fa14c0b3f55a7a1e734a988c5 to your computer and use it in GitHub Desktop.
Save wilg/c13c6b1fa14c0b3f55a7a1e734a988c5 to your computer and use it in GitHub Desktop.
extension Task where Success == Void, Failure == Error {
@discardableResult
/// A task that performs a non-throwing `operation`.
static func strict<S>(priority: TaskPriority? = nil, operation: @escaping @Sendable () async -> S) -> Self {
Task(priority: priority, operation: {
_ = await operation()
})
}
}
extension Task where Success == Void, Failure == Error {
@discardableResult
/// A task that performs `operation`, and alerts any errors thrown.
static func alertingError<S>(priority: TaskPriority? = nil, operation: @escaping @Sendable () async throws -> S) -> Self {
Task(priority: priority, operation: {
do {
_ = try await operation()
} catch {
alertError(error)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment