Skip to content

Instantly share code, notes, and snippets.

@hoc081098
Created March 24, 2022 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoc081098/f80a191c546077e567a7210d68b2bdbb to your computer and use it in GitHub Desktop.
Save hoc081098/f80a191c546077e567a7210d68b2bdbb to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
public fun <T> race(flows: Iterable<Flow<T>>): Flow<T> = TODO("Will implement")
fun firstSource(): Flow<String> = flow {
delay(400)
repeat(10) {
emit("[1] - $it")
delay(100)
}
}
fun secondSource(): Flow<String> = flow {
delay(100)
repeat(10) {
emit("[2] - $it")
delay(100)
}
}
fun thirdSource(): Flow<String> = flow {
delay(500)
repeat(10) {
emit("[3] - $it")
delay(100)
}
}
suspend fun main() {
race(
firstSource(),
secondSource(),
thirdSource()
).collect { println(it) }
// CONSOLE
// [2] - 0
// [2] - 1
// [2] - 2
// [2] - 3
// [2] - 4
// [2] - 5
// [2] - 6
// [2] - 7
// [2] - 8
// [2] - 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment