kotlin coroutine channel code example
Example: kotlin coroutine channel
Channels
fun CoroutineScope.produceSquares():
ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
val squares = produceSquares()
repeat(5) { println(squares.receive()) } // 1, 4, 9, 16, 25
val squares2 = produceSquares()
for(square in squares2) print(square) // 1, 4, 9, 16, 25