I'm trying to test out some async stream but I'm not sure I am understanding them correctly. I have the following two example functions:
func foo() async throws { print("Start") for await data in bar() { print(data) } print("End") } private func bar() -> AsyncStream<String> { return AsyncStream { continuation in print("Stream Started") for count in 0...2 { sleep(1) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } } I would have expected this to print something like this:
Start Stream Started Yielding... 0 Yielding... 1 Yielding... 2 End However what I am seeing is:
Start Stream Started Yielding... Yielding... Yielding... 0 1 2 End Is my expectation wrong here on how this stream should work?
sleepthis antique way. You undermine the async functionality. There is a native non-blocking way to wait for a moment.sleep()or the corrected version withTask.sleep(for:). ;)