Would buffered flows be the current best way of fa...
# coroutines
m
Would buffered flows be the current best way of fanning out a number of (comparatively) long running http requests and aggregating the results?
c
How would a buffered flow do that? I woulnd’t use a flow at all. Just launch them all with
async
, and
await
all those after they’ve all been launched:
Copy code
val responses = listOf<Request>()
    .map { async { it.exec() } } // launch each HTTP request in parallel
    .map { it.await() } // wait for the responses to come in
m
Yeah I just realised it wouldn';t it would just buffer slow collectors
c
Alternatively, pushing the requests into a Channel and setting up “worker” coroutines to pull from that channel with a select expression can give you more control over the level of parallelism you want. Probably would want to push the results into another channel to aggregate them all https://kotlinlang.org/docs/select-expression.html
m
Channels would provide better support for cancellation if I wanted to do a "fail fast" type scenario where I wanted to cancel everything if one failed