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

René

05/16/2019, 11:36 AM
hey guys, short question: Is there a way to define a block which is a suspend function?
Copy code
suspend fun magic(token: String) {}

fun tokenMagic(refreshToken: String, work: (String)->Unit) =
        runBlocking {
            fetchToken(refreshToken)
        }?.let(work::invoke)

fun example() {
    tokenMagic("42") { token ->
        magic(token) // magic should be only called from a coroutine
    }
}
I would like to execute "work" within a runBlocking block, so that I doesn't need to add a runBlocking statment for each usage of
tokenMagic
m

marstran

05/16/2019, 11:39 AM
Mark the lambda with the
suspend
modifier:
work: suspend (String) -> Unit
r

René

05/16/2019, 11:40 AM
oh really there the keyword should be there. I feel so stupid 😄
yes it seems to work thank you!
m

marstran

05/16/2019, 11:43 AM
No problem 👍
3 Views