next

abstract operator fun next(): E(source)

Retrieves the element removed from the channel by the preceding call to hasNext, or throws an IllegalStateException if hasNext was not invoked.

This method can only be used together with hasNext:

while (iterator.hasNext()) {
val element = iterator.next()
// ... handle the element ...
}

A more idiomatic way to iterate over a channel is to use a for loop:

for (element in channel) {
// ... handle the element ...
}

This method never throws if hasNext returned true. If hasNext threw the cause with which the channel was closed, this method will rethrow the same exception. If hasNext returned false because the channel was closed without a cause, this method throws a ClosedReceiveChannelException.