-
-
Save pixlwave/7ef018849250ec48f8869d21073ad0a0 to your computer and use it in GitHub Desktop.
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
import Foundation | |
extension Task where Failure == Never { | |
public static func dispatched(on queue: DispatchQueue, | |
priority: TaskPriority? = nil, | |
operation: @escaping @Sendable () -> Success) -> Task<Success, Failure> { | |
Task.detached(priority: priority) { | |
await withCheckedContinuation { continuation in | |
queue.async { | |
continuation.resume(returning: operation()) | |
} | |
} | |
} | |
} | |
} | |
extension Task where Failure == Error { | |
public static func dispatched(on queue: DispatchQueue, | |
priority: TaskPriority? = nil, | |
operation: @escaping @Sendable () throws -> Success) -> Task<Success, Failure> { | |
Task.detached(priority: priority) { | |
try await withCheckedThrowingContinuation { continuation in | |
queue.async { | |
do { | |
let result = try operation() | |
continuation.resume(returning: result) | |
} catch { | |
continuation.resume(throwing: error) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment