Created
October 24, 2021 20:25
-
-
Save joshuajhomann/3077e98a0aa77302f664913bb42a1e20 to your computer and use it in GitHub Desktop.
This file contains 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
extension Future where Failure == Error { | |
convenience init(awaiting operation: @escaping () async throws -> Output) { | |
self.init { promise in | |
Task { | |
do { | |
let value = try await operation() | |
promise(.success(value)) | |
} catch { | |
promise(.failure(error)) | |
} | |
} | |
} | |
} | |
} | |
extension Task where Failure == Error { | |
init(priority: TaskPriority? = nil, completion: @escaping ( @escaping (Result<Success, Error>) -> Void) -> Void) { | |
self.init(priority: priority) { | |
try await withCheckedThrowingContinuation { continuation in | |
completion(continuation.resume(with:)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment