iterator

abstract operator fun iterator(): ChannelIterator<E>(source)

Returns a new iterator to receive elements from this channel using a for loop. Iteration completes normally when the channel is closed for receive without a cause and throws the exception passed to close if there was one.

Instances of ChannelIterator are not thread-safe and shall not be used from concurrent coroutines.

Example:

val channel = produce<Int> {
repeat(1000) {
send(it)
}
}
for (v in channel) {
println(v)
}

Note that if an early return happens from the for loop, the channel does not get cancelled. To forbid sending new elements after the iteration is completed, use consumeEach or call cancel manually.