It would be great if our coroutines could always make decisions by themselves. But what if they need to return some results from the computation to another coroutine?
The opposite of fan-out is the fan-in design pattern. Instead of multiple coroutines reading from the same channel, multiple coroutines can write their results to the same channel.
Imagine that you're reading news from two prominent tech resources: techBunch and theFerge.
Each resource produces the values at its own pace, and sends them over a channel:
private fun techBunch(collector: Channel<String>) = launch {
repeat(10) {
delay(Random().nextInt(1000))
collector.send("Tech Bunch")
}
}
private fun theFerge(collector: Channel<String>) = launch {
repeat(10) {
delay(Random().nextInt(1000))
collector.send("The Ferge")
...