When I try to run this code, an error of `return i...
# android
o
When I try to run this code, an error of
return is not allowed here
is shown. I’ve read that Non-labeled return statements return from the nearest enclosing fun (ignoring lambdas). I don’t know what’s the mistake here. Could anyone help please?
a
return@postDelayed
0 ?
o
@Aregev2 No, to the doSomething function
@Aregev2 I want to return the result to the doSomething() function and not to the postDelayed funtion
l
@ossama You are looking for kotlinx.coroutines, there's no way to achieve what you're looking for without coroutines
o
Didn’t know that. Thanks @louiscad 🙂
l
Using kotlinx.coroutines, you'll no longer need to write such a method yourself, you can just use
UI
CoroutineContext
instead for this use case
o
I have never worked with coroutines. I have to check them out then 👍
g
@ossama in general your approach is wrong by definition. You cannot magically return async result from sync function, so, you have a few options: 1. block current thread for 2 seconds using Thread.sleep(2000) but be careful with main thread 2. use callback to return result, standard approach for such cases on Android 3. use coroutines, which allow you to write asyncronous code that looks like synchronous
👍 2
o
Thanks @gildor 😉