How can this be rewritten in a better way wrt stru...
# coroutines
r
How can this be rewritten in a better way wrt structured concurrency and flow etc
Copy code
fun <A, B> Iterable<A>.parallelMap(context: CoroutineContext, f: suspend (A) -> B): List<B> = runBlocking {
    map { async(context) { f(it) } }.map { it.await() }
}

@OptIn(ExperimentalCoroutinesApi::class)
private val apiPool = Dispatchers.IO.limitedParallelism(10)

val results = foo.parallelMap(apiPool) { fetchStuffFromRestAPI(it.id) }
s
For structured concurrency it would be best to change the
parallelMap
function to a
suspend
function:
Copy code
suspend fun <A, B> Iterable<A>.parallelMap(f: suspend (A) -> B): List<B> = coroutineScope {
    map { async { f(it) } }.map { it.await() }
}
I wouldn’t introduce flows here; I don’t think it would help you
You could also swap
.map { it.await() }
for
.awaitAll()
which will fail fast if one of the child jobs fails — depends on your approach to the structured concurrency
r
if I want to convert failures to null, just try-catch inside the lambda is cleanest?
s
Yes 👍