Created
April 17, 2025 08:54
-
-
Save jacobsapps/2547fc63e2cc84b22206e7b4e0df2dd6 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
func downloadStream() -> AsyncThrowingStream<Double, Error> { | |
AsyncThrowingStream { continuation in | |
Task { | |
var percentage = 0.0 | |
while percentage < 70 { | |
try await Task.sleep(for: .milliseconds(10)) | |
let bits = Double.random(in: 0...0.3) | |
percentage += bits | |
continuation.yield(with: .success(percentage)) | |
} | |
continuation.yield(with: .failure(.failedToDownload)) | |
continuation.finish() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment