what is the equivalent in Kotlin for await Promise...
# coroutines
b
what is the equivalent in Kotlin for await Promise.all()
1
j
If you have a list of
Deferred
, you can use
list.awaitAll()
on it.
But using
coroutineScope
on its own to wrap all your coroutines is sometimes enough, because it waits for child coroutines by default
b
Copy code
objects.asFlow()
        .map { it.getRequest().await() }
        .toList()
or is that a bad idea?
j
What does
getRequest()
return?
b
a promise
I know there's an all method on it, was just interested in the more generic solution
j
Ah, then this code would work, but it will start and await each request one by one (they will not run concurrently)
b
oh I see
so flows are sequential in that case
kinda like sequences: one element processed at a time
I guess this then inside a suspending function:
Copy code
objects
        .map { it.getRequest().asDeferred() }
        .awaitAll()
j
Yeah I don't think you need a flow here. Your last snippet is a great option
b
thank you
114 Views