Skip to content

Instantly share code, notes, and snippets.

@manoamaro
Created April 5, 2020 13:19
Show Gist options
  • Save manoamaro/140505673052e13def45c3994a3e7867 to your computer and use it in GitHub Desktop.
Save manoamaro/140505673052e13def45c3994a3e7867 to your computer and use it in GitHub Desktop.
kt-coroutines-flow-01
fun randomIntFlow(): Flow<Int> = flow { //flow builder
// suspend functions can be called from inside the flow
for (i in 1..10) {
emit(Random.nextInt()) // emit the value
delay(1000) //suspend function
}
}
runBlocking {
randomIntFlow().collect { i -> println(i) } // collect() is a suspend function.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment