Skip to content

Instantly share code, notes, and snippets.

@vprabhu
Last active August 20, 2024 05:39
Show Gist options
  • Save vprabhu/04cd8f6eaeecaf0d3de72049d023eddf to your computer and use it in GitHub Desktop.
Save vprabhu/04cd8f6eaeecaf0d3de72049d023eddf to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
suspend fun main(): Unit = coroutineScope {
val simpleFlow = flow {
delay(1500)
emit(1)
delay(1500)
emit(2)
// throw Exception("Network Exception")
delay(1500)
emit(3)
}
launch {
simpleFlow.onStart {
println("Start block where we can update the UI like starting the progressbar ")
}.onEach { item ->
val result = item + 100
println("Each item received $result")
}.onCompletion { cause ->
println("The flow emission is done. Stop the progressbar $cause")
/**
* if u comment this line throw Exception("Network Exception")
* output : The flow emission is done. Stop the progressbar java.lang.Exception: Network Exception
* otherwise
* output : The flow emission is done. Stop the progressbar null
*/
}.collect {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment