A channel can be closed either by the sender calling
close()
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.