myanmarking
09/25/2023, 10:31 AMSam
09/25/2023, 10:48 AMconsumeEach will call cancel() on the channel after you're done iterating. The main situation where that differs from a regular for loop is if the iteration ends early, for example due to an exception or control flow statement in the loop body. A for loop can stop iterating while leaving the channel open for future items, but consumeEach will always leave the channel in a closed state.Sam
09/25/2023, 10:49 AMmyanmarking
09/25/2023, 10:51 AMmyanmarking
09/25/2023, 10:51 AMSam
09/25/2023, 10:55 AMclose() or by the consumer calling cancel(). You're right that consumeEach won't normally return until the sender closes the channel, but there are cases where that's not true. For example:
channel.consumeEach {
throw Exception("There was an error")
}
If an exception is thrown from consumeEach, the iteration stops and won't process any more items. In that case, the consumeEach function will automatically call cancel() to close the channel before letting the error propagate.myanmarking
09/25/2023, 10:56 AM