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

jean

02/08/2021, 10:22 AM
Copy code
private val supervisor = SupervisorJob()
private val coroutineScope = CoroutineScope(coroutineContext + supervisor)
...
fun onEvent(event: MyEvent) {
    coroutineScope.launch {
        val data = findHeavyWorkToExecute()?.invoke()
            ?: throw Exception(currentState, event)

        someMoreWork(data)
     }
}
I’m want
someMoreWork()
to be executed only if the lambda returned by
findHeavyWorkToExecute()
is not null and its execution does not fail. But when I test this
someMoreWork()
is called even though the
throw
statement occurs. What am I suppose to change there?
w

wasyl

02/08/2021, 10:27 AM
Do you use new JVM IR backend perhaps?
j

jean

02/08/2021, 10:32 AM
no I didn’t updated yet
w

wasyl

02/08/2021, 11:05 AM
Are you sure
findHeavyWorkToExecute()?.invoke()
returns null? This simple test doesn’t print anything:
Copy code
private val coroutineScope = CoroutineScope(SupervisorJob())

@Test
fun test(): Unit = runBlocking {
    coroutineScope.launch {
        val data = findSomeHeavyWork() ?: throw Exception()

        println("after stuff")
    }

    delay(1000)
}

fun findSomeHeavyWork(): Unit? = null
j

jean

02/08/2021, 11:13 AM
you are right, I just realized it was the lambda execution that was failing inside a try/catch and returning an error rather than the lambda being null 🤦 thanks for the help!
👍 1
12 Views