Skip to content

A proposal for awaitAny and joinAny #3664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ShreckYe opened this issue Mar 7, 2023 · 2 comments
Closed

A proposal for awaitAny and joinAny #3664

ShreckYe opened this issue Mar 7, 2023 · 2 comments

Comments

@ShreckYe
Copy link

ShreckYe commented Mar 7, 2023

I'd like to propose a set of awaitAny and joinAny functions, which serve as counterparts to awaitAlls and joinAlls and are shortcuts to select in some cases. They can easily be implemented with select like this:

suspend fun <T> awaitAny(vararg deferreds: Deferred<T>): T =
    select { deferreds.forEach { it.onAwait { it } } }

suspend fun <T> Collection<Deferred<T>>.awaitAny(): T =
    select { forEach { it.onAwait { it } } }

suspend fun joinAny(vararg jobs: Job): Unit =
    select { jobs.forEach { it.onJoin { it } } }

suspend fun Collection<Job>.joinAny(): Unit =
    select { forEach { it.onJoin { it } } }
@JakeWharton
Copy link
Contributor

See #2867. Your implementation allows losing coroutines to continue to do work despite their result never being used.

@dkhalanskyjb
Copy link
Collaborator

Yep, duplicate of #2867

@dkhalanskyjb dkhalanskyjb closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants