```launch { val result = getSomething().apply ...
# coroutines
j
Copy code
launch {
    val result = getSomething().apply {
        doSomethingSuspending()
    }
    // code at this point never runs
}
Could anyone explain why this happens?
w
Most likely
doSomethingSuspending()
never completes
👆 1
g
If I understand this correctly, doSomethingSuspending never returns
l
Your
doSomethingSuspending()
is a fancy version of
awaitCancellation()
Or, your
getSomething()
is that one.
j
Code after doSomethingSuspending() inside the apply block executes, but code outside the apply block doesnt. That means it completes right?
w
Yes, but it shouldn’t be happening. Could the code after doSomethingSuspending be throwing an exception?
l
Your snippet didn't show any code after
doSomethingSuspending()
@Jason Ankers It's kinda cheating! 😛
j
Theres no code after it - i just added a print statement to see if it executes
l
Are you on Android with AGP 7 alpha?
j
Yes
l
Try rebuilding the app, and if it still occurs, please try doing a Gradle clean, then building with the
--no-build-cache
option and .
j
The backstory is I had some code like this:
Copy code
launch {
    val result = getSomething()
    result.doSomethingSuspending()
    // do stuff
}
Which I changed to this:
Copy code
launch {
    val result = getSomething().apply {
        doSomethingSuspending() 
    }
    // do stuff
}
but after making the change,
result
never gets assigned, and any code after it never runs
So in theory that should work fine?
l
Yes it should, but please, try what I asked and answer these, I'd like to know if it's an issue in AGP 7 alpha I've been witnessing
j
Ok will do
Didn’t work after any of that
l
Can you wrap the whole thing with a try catch finally block that logs before rethrowing and in the finally block?
Plus insert a log before all this code to ensure the changes are deployed
j
Ok yep the function is throwing something 🙂
l
Łukasz Wasylkowski was right the whole time on his first message.
j
Yep - apologies for not trying that first. I had just assumed if the function was throwing it wouldn’t have worked in the original implementation. Any idea why that might have been happening?
I.e:
Copy code
launch {
    val result = getSomething()
    result.doSomethingSuspending()
    // code was running here fine
}
l
Unrelated change or repeated execution, or who knows, we don't have your codebase at hand 😄