# anyOf equivalent of Kotlin Deffered

**URL:** <https://discuss.kotlinlang.org/t/anyof-equivalent-of-kotlin-deffered/7313>\
**Category:** Support\
**Created:** [April 4, 2018, 1:37am UTC](https://discuss.kotlinlang.org/t/anyof-equivalent-of-kotlin-deffered/7313 "2018-04-04T01:37:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vach](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vach/32/6909_2.png) [@vach](https://discuss.kotlinlang.org/u/vach)\
**Post date:** [April 4, 2018, 1:37am UTC](https://discuss.kotlinlang.org/t/anyof-equivalent-of-kotlin-deffered/7313/1 "2018-04-04T01:37:22Z")

</div>

Coroutine async returns Deferred and there are example of lazy execution and usages of await.

However how can we wait for any of the Deffered instances to complete?

In a nutshell

```
  // whats the equivalent of CompletableFuture.anyOf(...)?
  // is this how we do it? if so how costly is this?
  select<Unit> {
     deffered1.onAwait {}
     deffered2.onAwait {}
  }

```

---

<div class="post-metadata">

**Author:** ![nottheoilrig](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nottheoilrig](https://discuss.kotlinlang.org/u/nottheoilrig)\
**Post date:** [May 27, 2021, 5:20pm UTC](https://discuss.kotlinlang.org/t/anyof-equivalent-of-kotlin-deffered/7313/2 "2021-05-27T17:20:36Z")

</div>

[`.merge(): Flow<T>`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/kotlin.collections.-iterable/merge.html) is an alternative to `select()`. I’ve used it to address [a similar problem](https://github.com/Kotlin/kotlinx.coroutines/issues/171#:~:text=get%20the%20first%20in%20a%20collection%20of%20deferreds%20to%20complete%20with%20a%20non-null%20value) (get the first in a collection of deferreds to complete with a non-null value). i.e.

```kotlin
deferreds.map { it::await.asFlow() }.merge().first()
```
