Are there situations in which you, instead of using a suspend function, use a regular function that returns a Flow that emits a single value? Maybe to use some flow operators that make the code a bit cleaner?
r
Robert Williams
08/24/2022, 11:55 AM
It’s bad practice to create APIs based on how you assume they’ll always be used and a flow that emits exactly once is a confusing API
Robert Williams
08/24/2022, 11:56 AM
If the consumer decides it’s more convenient to work with flow it’s pretty simple for them to convert:
Copy code
flow {
emit(suspendingFun())
}
s
Sam
08/24/2022, 11:57 AM
Even simpler,
(suspend () -> T).asFlow()
is in the coroutines stdlib so you can write
Copy code
::suspendingFun.asFlow()
r
Robert Williams
08/24/2022, 12:01 PM
My favourite thing about kotlin is that you learn a new extension function everyday 😅