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

Oleh Havrysh

05/21/2019, 9:39 AM
Hi! I want to wrap my player callback into suspend function that will just suspend until something completed without returning any value. Is there some alternative for
suspendCoroutine
that doesn’t require passing result value?
I can rewrite it as
Copy code
private suspend fun waitForPlayerOpen() {
        suspendCoroutine<Unit> {
            player.addOpenListener {
                it.resume(Unit) //what if I don't need to return value?    
            }
        }
    }
But it still looks as workaround, isn’t it?
g

gildor

05/21/2019, 9:46 AM
Pass Unit
it’s not a workaround, it’s exactly how it should work for functions that return Unit (which is any function “without returning any value.“, just language allows you to omit return type if it’s Unit
o

Oleh Havrysh

05/21/2019, 9:50 AM
Great, thank you!
m

Melih Aksoy

05/21/2019, 10:02 AM
How about
it.resumeWith(Result.success)
? It’s still a return value but more meaningful.
o

Oleh Havrysh

05/21/2019, 10:08 AM
success
is also function that takes value and wraps it into
Result
, so I need to write
it.resumeWith(Result.success(Unit))
. It almost the same as previous variant
g

gildor

05/21/2019, 10:27 AM
It almost the same as previous variant
It’s exactly the same, function
resume
is juste extension function that wraps object to Result.success
m

Melih Aksoy

05/21/2019, 1:07 PM
Ah you’re right
4 Views