I'm trying to implement a custom worker pool using...
# coroutines
n
I'm trying to implement a custom worker pool using channels. Consider the pseudocode below:
Copy code
class WorkerPool {
    val chanToWorkers = Channel<InputType>()
    val chanFromWorkers = Channel<Pair<InputType, OutputType>>()

    suspend fun doStuff(input: InputType): OutputType {
        // Send input to workers to process
        chanToWorkers.send(input)

       // ??? What should I use to wait for the output?
    }
}
I'm stuck since I need some way to suspend until the worker has processed the input. Is there any
Channel.receiveIf {}
function so I can do something like the below?
Copy code
suspend fun doStuff(input: InputType): OutputType {
    // Send input to workers to process
    chanToWorkers.send(input)

    return chanFromWorkers.receiveIf { it.first == input }.second
}
Am I thinking about this wrong?