Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created April 17, 2025 08:54
Show Gist options
  • Save jacobsapps/2547fc63e2cc84b22206e7b4e0df2dd6 to your computer and use it in GitHub Desktop.
Save jacobsapps/2547fc63e2cc84b22206e7b4e0df2dd6 to your computer and use it in GitHub Desktop.
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