I have the following use case: My function returns...
# coroutines
r
I have the following use case: My function returns a flow.. for simplicity lets say it returns
Flow<Int>
In this function I have an api call which also returns a flow What is the best way to give result back to the caller? Sample code in thread
Copy code
fun apiCall(): Flow<Int> = flow {
    delay(2000) // some network call
    emit(3)
}
Copy code
fun myFunc(): Flow<Int> = flow {
    emit(1) //denote loading
    apiCall() 
    
    //is the only way collecting here and emitting again?

  // apiCall().collect { emit(it) }
}
call site
Copy code
myFunc().collect {
    //do something with each result
}
j
Why does
apiCall
return a flow of one element? One-shot calls are way easier to manipulate as simple suspend functions
If you really really want a flow from the API call, you could use
emitAll
I guess, but I would really reconsider the
apiCall
function first
r
understood.. making it a one-shot call will be better 👍