We have a case where we need coroutines to work wi...
# coroutines
h
We have a case where we need coroutines to work with a Java library with lambdas in-between two Kotlin libraries with coroutines. What would be the most elegant way to make this snippet work?
g
You should write coroutine adapter that will replace this callback with suspend function (extension for this type)
h
@gildor Great info thanks. Just one additional question if you do know. The wrapping examples they give don’t invoke another chained suspended method within the callback. And in my case the callback is also expecting a return value to do some metrics on. JavaLibraryNotTakingSuspend.get uses the return value of kotlinLibrary.methodWithSuspend().
g
No-no, see, you convert your get to something like getAwait(), than just call it, get result and call methodWithSuspend
No need to move methodWithSuspend to adapter
So you will have:
Copy code
val result = someJava.getSuspend()
methodWithSuspend(result)
Just sequential-like code
h
I understand, but I’ll add some clarity. This is a circuit breaker library. So we have:
JavaCircuitBreaker { kotlinSuspendLib.someAsyncReq  == SUCCESS }
the CircuitBreaker that executes the
callback
expects a value back from the block in the
callback
The above works only if you expect the JavaLib to provide data, not expect data back.
g
Not quite sure what you mean
Maybe you cold give some self-contained example
h
@gildor Thanks, I’ll post a new snipit with everything in it.