Hi, how can I run concurrent coroutines and wait a...
# coroutines
m
Hi, how can I run concurrent coroutines and wait all are done (like Promise.all in JS) ? I see the method with a map which create
Deferred
then call
await()
:
Copy code
data.map {
                async(Bg) { it.httpGet().response() }
            }.map {
                it.await()
            }
Is it the good approach ?
l
@mmaillot Yes, it is!
m
Good, thank you! Do you know how to write a race condition ? I mean await until the first coroutines is done.
g
Also there is a proposal to add extension function for such cases: https://github.com/Kotlin/kotlinx.coroutines/issues/171 But for now you can write such function yourself
Race condition? What do you mean? To wait when first coroutine is done just call await on first coroutine. But actually this code already does this
m
Like Promise.race in JS, I want to wait until one of my coroutine is complete (not nessary the first but the fastest)
u
you can use
select
together with
Deferred.onAwait
for that