-
Couldn't load subscription status.
- Fork 24
About Async and Await
- Since the
launch+joincombo does not return a result, We would need to share the state between the coroutines which would cause issues when multiple co-routines would share the state and cause issues. - Bugs caused by shared mutable state would result in bugs that are hard to fix.
Basically Async is used to compute the code asynchronously and Await is used to return the result of that computation. They are called in suspending function.
Here's a brief explanation of when to use async and await in Android Kotlin coroutines:
-
Use
asyncwhen you want to perform concurrent tasks:- When you have multiple independent tasks that can be executed concurrently, you can use
asyncto launch coroutines for each task. -
asyncreturns an instance ofDeferred, which is a lightweight non-blocking future representing a value that may or may not be available.
val result1: Deferred<ResultType1> = async { // ... } val result2: Deferred<ResultType2> = async { // ... }
- When you have multiple independent tasks that can be executed concurrently, you can use
-
Use
awaitto retrieve the results:- After launching multiple
asynccoroutines, you can useawaitto retrieve the results of each coroutine when needed. -
awaitis a suspending function, meaning it can only be called from within a coroutine or another suspending function.
val result1: ResultType1 = result1.await() val result2: ResultType2 = result2.await()
- After launching multiple
-
Use
asyncandawaitwithin asuspendfunction:- It's common to use
asyncandawaitwithin asuspendfunction to perform asynchronous operations. - The
asynccoroutines can be launched and awaited within asuspendfunction, allowing you to structure your asynchronous code more sequentially.
suspend fun performAsyncTasks(): CombinedResult { val result1: Deferred<ResultType1> = async { /* ... */ } val result2: Deferred<ResultType2> = async { /* ... */ } val combinedResult = CombinedResult(result1.await(), result2.await()) return combinedResult }
- It's common to use
Remember that coroutines are generally used to handle asynchronous operations in a more structured and readable way, making the code easier to reason about. Always be mindful of the coroutine context, and try to avoid blocking operations within coroutines whenever possible.
It helps to concurrently run multiple tasks and get the result from all the tasks
code
class UsingAsyncAwaitDemoVm @Inject constructor( ) : ViewModel() {
private val scopeJob = Job()
private val ourScope = CoroutineScope(scopeJob + Dispatchers.Default)
fun demo() {
try {
ourScope.launch(CoroutineName("GrandParent")) {
val endResult = (1..15).toList().map { inputValue ->
async {
delay(100)
inputValue
}
}.awaitAll().sum()
println("Final Result:-> $endResult")
}
}catch (ex:Exception){
println("Exception caught outside GrandParent scope")
}
}
fun rootCancel() {
scopeJob?.cancel()
}
}Output
Final Result:-> 120