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

Pablo

07/05/2022, 9:17 AM
Hello how can I transform a callback to a suspend fun?
Copy code
interface Foo<T> {
  fun onSuccess(lis: (T?) -> Unit)
}
I'm trying to do something like this :
Copy code
suspend fun getFoo(): Bar {
   suspendCoroutine<Bar?> { continuation -> 
     val listener = object : Foo<Bar?> {
        override fun onSuccess(lis: (Bar?) -> Unit) { }
     }
}
But I don't know how to return a Bar
j

Joffrey

07/05/2022, 9:22 AM
Your
onSuccess
is probably not a callback, but a function to register a callback. I don't think it's expected that you create an instance of
Foo
yourself here. Could you please share the real-life names for those types? Coroutines aside, how is
Foo
supposed to be used with the callback approach?
6 Views