consume

fun <E> CoroutineScope.consume(capacity: Int = Channel.RENDEZVOUS, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, onUndeliveredElement: (E) -> Unit? = null, consumer: suspend ReceiveChannel<E>.() -> Unit): Channel<E>

Creates a Channel with the specified capacity, buffer overflow behavior, and undelivered element handler, and then consumes elements from this channel using the given consumer function.

As soon as the Channel or the Job finishes, one closes the other.

Return

The created Channel which the consumer is consuming from.

Example usage:

coroutineScope {
val channel = consume<Int> {
for (item in this) {
println(item)
}
}

channel.send(1)
channel.send(2)
}

Parameters

capacity

The capacity of the channel. Defaults to Channel.RENDEZVOUS.

onBufferOverflow

Specifies what to do when the buffer overflows. Defaults to BufferOverflow.SUSPEND.

onUndeliveredElement

Optional handler for undelivered elements. This is invoked when an element cannot be delivered to the consumer for any reason.

consumer

A lambda with receiver of type ReceiveChannel which defines how to consume elements from the channel.