Iād like to ask a question about return in asyncstream. it seems to stop the stream.
but why I cant get any event like cancelled or finished in continuation.onTermination closure.
So this means the asyncstream hasnt ended.
AsyncStream<String> { continuation in
continuation.onTermination = { state in
switch state {
case .cancelled:
print("cancelled")
case .finished:
print("finished")
@unknown default:
print("unknown")
}
}
var countdown = 3
Timer.scheduledTimer(
withTimeInterval: 1.0,
repeats: true
) { timer in
guard countdown > 0 else {
timer.invalidate()
continuation.yield(with: .success("š done"))
continuation.finish()
return
}
// here
if countdown == 2 {
return
}
continuation.yield("\(countdown) loading...")
countdown -= 1
}
}