I need to adapt a suspending function that returns...
# coroutines
s
I need to adapt a suspending function that returns
Flow
to a non-suspending function that returns
Flow
. Is this the correct way to do that?
Copy code
suspend fun suspendingFlow(): Flow<String> { ... }

fun nonSuspendingFlow() = flow {
	suspendingFlow().collect {
		emit(it)
	}
}
v
Not really.
suspendingFlow()
will be invoked on every
collect
and it is probably not expected behaviour
s
What is your recommandation for that use case?
v
There are no good options. Suspend modifier inherently means that function is asynchronous, so it can’t be simply converted to a plain Java method. You should either return something asynchronous and stateful or use e.g.
runBlocking
s
Ok so I can maybe try to use
Mono
and
flatMapMany
(my use case end up by processing a
Flux
)