Skip to content

Instantly share code, notes, and snippets.

@wilg
Last active Nov 9, 2021
Embed
What would you like to do?
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