https://kotlinlang.org logo
#coroutines
Title
# coroutines
h

Herko

02/22/2019, 3:29 PM
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

gildor

02/22/2019, 5:00 PM
You should write coroutine adapter that will replace this callback with suspend function (extension for this type)
h

Herko

02/22/2019, 5:22 PM
@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

gildor

02/22/2019, 5:24 PM
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

Herko

02/22/2019, 5:26 PM
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

gildor

02/22/2019, 5:30 PM
Not quite sure what you mean
Maybe you cold give some self-contained example
h

Herko

02/22/2019, 5:31 PM
@gildor Thanks, I’ll post a new snipit with everything in it.
4 Views