diff --git a/lynglib/src/commonTest/kotlin/CoroutinesTest.kt b/lynglib/src/commonTest/kotlin/CoroutinesTest.kt index 09345d3..a597c96 100644 --- a/lynglib/src/commonTest/kotlin/CoroutinesTest.kt +++ b/lynglib/src/commonTest/kotlin/CoroutinesTest.kt @@ -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(""" diff --git a/lynglib/stdlib/lyng/root.lyng b/lynglib/stdlib/lyng/root.lyng index dff3de8..a79458f 100644 --- a/lynglib/stdlib/lyng/root.lyng +++ b/lynglib/stdlib/lyng/root.lyng @@ -29,6 +29,16 @@ extern class Deferred { val isCancelled: Bool } +/* Await every task in order and return collected results. */ +fun Iterable.joinAll(): List { + var results: List = 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