question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`awaitFirst` function

See original GitHub issue

Core 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

6reactions
qwwdfsadcommented, Nov 6, 2018

Closing as obsolete

2reactions
elizarovcommented, Sep 10, 2018

@SolomonSun2010 We’ve designed Kotlin coroutines for more ergonomics. In Kotlin these examples look like this:

for (request in requestServer) {
    handleRequest(request)
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found