Redundant 'async' call may be reduced to 'kotlinx.coroutines.withContext'
See original GitHub issueI have the following code:
DATABASE_CONTEXT.async(block = block).await()
IntelliJ gives me a warning and wants to simplify it to use withContext
instead of async
.
These two things aren’t exactly equivalent though, right? i.e. if I cancel the coroutine, it will also stop the coroutine code from executing if I user withContext
, but if I use DATABASE_CONTEXT.async(block = block).await()
, that has a new coroutine hierarchy, and that will ensure that the code inside the block runs to completion, right?
Those are two very important distinctions, and I like to differentiate between the use-cases. The fact that the IDE is suggesting these to me as equivalent seems like an error to me.
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (14 by maintainers)
Top Results From Across the Web
Kotlin: withContext() vs Async-await - Stack Overflow
async (context) : Starts a new coroutine in the given context and if we call .await() on the returned Deferred task, it will...
Read more >Kotlin withContext() vs. async-await - Baeldung
Coroutines are strong tools for writing asynchronous code with a fluent API in a sequential style without the headache of reactive style coding....
Read more >Settling the Async-Await v withContext debate in Kotlin ...
One such debate is the Async-Await v withContext to perform background work. ... Redundant 'async' call may be reduced to 'kotlinx.coroutines.withContext'.
Read more >Consider removing "Redundant 'async' call may be reduced ..."
This inspection and further quick-fix produce non-equivalent code, we should consider removing it completely. suspend fun test(ctx: CoroutineContext, ...
Read more >withContext - Kotlin
suspend fun <T> withContext(context: CoroutineContext, block: suspend ... Calls the specified suspending block with a given coroutine context, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks a lot! It is also good to know that this architecture works well with
withContext
. As for “wrapping plugin” – we indeed periodically run into cases where this or that kind of wrapping might be needed and some day this kind of “auto-wrapping” might become a separate language feature.It gets stuck anyway, but in case of
async
you just don’t wait for it on the outer scope. So this behaviour is rather uncovered a potential leak in your code. If JMS supports interruption, I would recommend usingrunInterruptible
to support cooperative cancellation of JMS