I have a function like this:
fun getNumbers2(input: Int): Flow<Int> = callbackFlow {
for (i in 1..5) {
send(i + input)
delay(1000)
}
close()
}
I want that Flow
to return another Flow
from inside a Flow
function. How can I achieve something like the following snippet?
fun getNumbers1(): Flow<Int> = callbackFlow {
val input = 2
send(input)
return getNumbers2(input) // do we have any resumeWith() or continueWith() function in Kotlin Flow??
}
The expectation is to achieve a coroutine like this:
GlobalScope.launch(Dispatchers.IO) {
getNumbers1().collect {
// it should be able to print results from getNumbers2() as well
println("Result: $it")
}
}
I don’t want to create a Flow
chain like this, because it does not suit my case:
GlobalScope.launch(Dispatchers.IO) {
getNumbers1().flatMapMerge { getNumbers2(it) }.collect {
println("Result: $it")
}
}
Thanks in advance.