Add Deferred.joinAll helper

This commit is contained in:
Sergey Chernov 2026-04-09 11:27:32 +03:00
parent 402b8bb1b3
commit c9eb3df93d
2 changed files with 29 additions and 0 deletions

View File

@ -120,6 +120,25 @@ class TestCoroutines {
)
}
@Test
fun testJoinAll() = runTest {
eval(
"""
val replies = (1..6).map { n ->
launch {
delay((7 - n) * 5)
"done:${'$'}n"
}
}.joinAll()
assertEquals(
["done:1", "done:2", "done:3", "done:4", "done:5", "done:6"],
replies
)
""".trimIndent()
)
}
@Test
fun testFlows() = runTest {
eval("""

View File

@ -29,6 +29,16 @@ extern class Deferred {
val isCancelled: Bool
}
/* Await every task in order and return collected results. */
fun Iterable<Deferred>.joinAll(): List<Object> {
var results: List<Object> = List()
for( task in this ) {
val deferred = task as Deferred
results += deferred.await()
}
results
}
/* A deferred result that can be completed manually. */
extern class CompletableDeferred : Deferred {
fun complete(value: Object): void