Skip to content

Instantly share code, notes, and snippets.

@detouched
detouched / coroutines-select.kt
Last active October 13, 2017 05:15
Kotlin Coroutines: select first _successful_ clause
/**
* Waits for the result of multiple suspending functions simultaneously. The caller is suspended until
* one of the clauses is _selected_ or all clauses _fail_.
*
* @param deferredList a list of [clauses][Deferred] to select a result from
* @param cancelOtherJobsWhenDone a flag whether all [Deferred] from the provided [deferredList] which are
* still active at the moment of getting the first result should be cancelled
* @return a [Pair] of a result of the _selected_ clause, if present, and a [List] of the _failed_ clauses
* gathered at the moment of getting that result. If all clauses _fail_, the result - a [Pair] of `null`

Keybase proof

I hereby claim:

  • I am detouched on github.
  • I am detouched (https://keybase.io/detouched) on keybase.
  • I have a public key ASD3G0bvIkNFg8HSmVrQurNnVpv5apdsRFVk6p8bDqT2cwo

To claim this, I am signing this object: