`awaitFirst` function
See original GitHub issueCore library misses of an Promise.race
equivalent function.
I propose a little POC of joinFirst
/awaitFirst
, or a more generic selectFirst
.
What do you think?
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.selects.SelectClause0
import kotlinx.coroutines.experimental.selects.SelectClause1
import kotlinx.coroutines.experimental.selects.select
import kotlin.reflect.KProperty1
fun main(args: Array<String>) = runBlocking {
val jobs = listOf(launch { delay(3) }, launch { delay(4) }, launch { delay(5) })
jobs.selectFirst(Job::onJoin) {
println("$it done")
}
val asyncs = listOf(async { 3 }, async { 4 }, async { 5 })
asyncs.selectFirst(Deferred<Int>::onAwait) { deferred, value ->
println("$deferred done with result $value")
}
println("First result is " + asyncs.awaitFirst())
}
// common functions
suspend fun <E : Job> Iterable<E>.joinFirst(): E = select {
for (job in this@joinFirst) {
job.onJoin { job }
}
}
suspend fun <E : Deferred<R>, R> Iterable<E>.awaitFirst(): R = joinFirst().getCompleted()
// generic funtions
suspend fun <E, R> Iterable<E>.selectFirst(selectProperty: KProperty1<E, SelectClause0>, block: (E) -> R): R =
select {
for (task in this@selectFirst) {
selectProperty.get(task).invoke { block(task) }
}
}
suspend fun <E, V, R> Iterable<E>.selectFirst(selectProperty: KProperty1<E, SelectClause1<V>>, block: (E, V) -> R): R =
select {
for (task in this@selectFirst) {
selectProperty.get(task).invoke { block(task, it) }
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:15 (13 by maintainers)
Top Results From Across the Web
What is the difference between await functions (coroutine) and ...
When you call subscribe on a reactive chain you decouple it from the main flow, it becomes independent and potentially asynchronous.
Read more >Comparing RxJava & Coroutines: Part 4 - Interop Library
await()` available on a Single, `.awaitFirst()` available on an Observable, etc. This is how you can use the `awaitFirstOrDefault(value)` extension function.
Read more >Kotlin Coroutines 1.5: GlobalScope Marked as Delicate ...
We added more functions for converting from Reactive Streams types to Kotlin Flow and back, stabilized many ... awaitFirst() and Mono.
Read more >Non-Blocking Spring Boot with Kotlin Coroutines - Baeldung
In order to transform functions from reactive to the Coroutines API, we add the suspend modifier before function definition:
Read more >JavaScript wait for function to finish tutorial - Nathan Sebhastian
await first(); second();. However, the await keyword must be used inside an async function . A function declared with the async ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing as obsolete
@SolomonSun2010 We’ve designed Kotlin coroutines for more ergonomics. In Kotlin these examples look like this:
It is cleaner and with less boiler-plate in Kotlin (no need to explicitly write
await
in Kotlin if you use suspending functions and channels). As an added bonus, it is not hardcoded into the language (unlike Dart), but is implemented in a library, so we can have even more variety of useful constructs without having to release an update to the language. I welcome you to study coroutines guide to learn about other features we offer.