I currently have a callback that instead of returning a value synchronously, it will need to do some async work and then return the result. I've been investigating about how to do it but I don't get thing clear. What I have achieved until now is this, but as I need to put a return into foo method, at first it returns "something" and afterthat return the result of calling someMethod().
Is it possible to to what I'm pretending?
class CustomClass() :
SomeClass.SomeInterface {
override fun foo(p0: SomeType): String {
val result = "something"
MainScope().launch {
withContext(Dispatchers.IO) {
result = someMethod()
}
}
return result
}
suspend fun someMethod(): String =
suspendCancellableCoroutine { cancelableContinuation ->
//TODO: register some listener that will end up calling
cancelableContinuation.resume("some_value", {})
}
Thanks in advance!