https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

mmaillot

03/29/2018, 2:59 PM
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

louiscad

03/29/2018, 3:05 PM
@mmaillot Yes, it is!
m

mmaillot

03/29/2018, 3:13 PM
Good, thank you! Do you know how to write a race condition ? I mean await until the first coroutines is done.
g

gildor

03/29/2018, 11:58 PM
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

mmaillot

03/30/2018, 5:40 AM
Like Promise.race in JS, I want to wait until one of my coroutine is complete (not nessary the first but the fastest)
u

uhe

03/30/2018, 8:59 AM
you can use
select
together with
Deferred.onAwait
for that
8 Views