Hello I have a simple question ```fun main() { ...
# coroutines
a
Hello I have a simple question
Copy code
fun main() {
    val observable = Observable.create<Int> { emitter ->
        for (i in 1..5) {
            emitter.onNext(i)
        }
        emitter.onComplete()
    }
    observable.asFlow().onEach {
        println("Flow: $it")
    }
}
Why this code doesn’t print anything?
o
onEach
is not a terminal operator, you probably want
collect
(extension function variant, not the internal coroutines API)
note that
collect
requires a
suspend
context, so you will probably also need
suspend fun main
a
I tried that
Copy code
fun main() {
    val observable = Observable.create<Int> { emitter ->
        for (i in 1..5) {
            emitter.onNext(i)
        }
        emitter.onComplete()
    }

    GlobalScope.launch {
        observable.asFlow().collect {
            println("Flow: $it")
        }
    }
}
It didn’t print anything
o
yes, because
launch
is async --
main
is exiting before
launch
gets to start
a
Aha okay .. thank you 🙏
m
Have you tried using
runBlocking
?
o
runBlocking
is another solution, it's similar to
suspend fun main
a
yes got it .. thanks guys