Launch vs Async in Kotlin Coroutines
Last Updated :
07 Apr, 2025
The Kotlin team defines coroutines as “lightweight threads”. They are sort of tasks that the actual threads can execute. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. Kotlin coroutines introduce a new style of concurrency that can be used on Android to simplify async code.
The official documentation says that coroutines are lightweight threads. By lightweight, it means that creating coroutines doesn’t allocate new threads. Instead, they use predefined thread pools and smart scheduling for the purpose of which task to execute next and which tasks later.
There are mainly two functions in Kotlin to start the coroutines.
Launch Function
The launch will not block the main thread, but on the other hand, the execution of the rest part of the code will not wait for the launch result since launch is not a suspend call. Following is a Kotlin Program Using Launch:
Kotlin
// Kotlin Program For better understanding of launch
fun GFG()
{
var resultOne = "Android"
var resultTwo = "Kotlin"
Log.i("Launch", "Before")
launch(Dispatchers.IO) { resultOne = function1() }
launch(Dispatchers.IO) { resultTwo = function2() }
Log.i("Launch", "After")
val resultText = resultOne + resultTwo
Log.i("Launch", resultText)
}
suspend fun function1(): String
{
delay(1000L)
val message = "function1"
Log.i("Launch", message)
return message
}
suspend fun function2(): String
{
delay(100L)
val message = "function2"
Log.i("Launch", message)
return message
}
When you will run the code in Android IDE, the log result will be:
Kotlin
// pseudo kotlin code for demonstration of launch
GlobalScope.launch(Dispatchers.Main)
{
// do on IO thread
fetchUserAndSaveInDatabase()
}
suspend fun fetchUserAndSaveInDatabase()
{
// fetch user from network
// save user in database
// and do not return anything
}
As the fetchUserAndSaveInDatabase() does not return anything, we can use the launch to complete that task and then do something on Main Thread.
When to Use Launch?
Launch can be used at places where users do not want to use the returned result, which is later used in performing some other work. For example, It can be used at places involving tasks like update or changing a color, as in this case returned information would be of no use.
Async Function
Async is also used to start the coroutines, but it blocks the main thread at the entry point of the await() function in the program. Following is a Kotlin Program Using Async:
Kotlin
// kotlin program for demonstration of async
fun GFG
{
Log.i("Async", "Before")
val resultOne = Async(Dispatchers.IO) { function1() }
val resultTwo = Async(Dispatchers.IO) { function2() }
Log.i("Async", "After")
val resultText = resultOne.await() + resultTwo.await()
Log.i("Async", resultText)
}
suspend fun function1(): String
{
delay(1000L)
val message = "function1"
Log.i("Async", message)
return message
}
suspend fun function2(): String
{
delay(100L)
val message = "function2"
Log.i("Async", message)
return message
}
One important point to note is that Async makes both of the networks call for result1 and result2 in parallel, whereas with launch parallel function calls are not made. When you will run the code in Android IDE, the log result will be:
When to Use Async?
When making two or more network call in parallel, but you need to wait for the answers before computing the output, ie use async for results from multiple tasks that run in parallel. If you use async and do not wait for the result, it will work exactly the same as launch.
Table of Differences
Below is the table of differences between Launch and Async:
Launch
| Async
|
---|
The launch is basically fire and forget. | Async is basically performing a task and return a result. |
launch{} does not return anything. | async{ }, which has an await() function returns the result of the coroutine. |
launch{} cannot be used when you need the parallel execution of network calls. | Use async only when you need the parallel execution network calls. |
Execution of other parts of the code will not wait for the launch result since launch is not a suspend call | Execution of the other parts of the code will have to wait for the result of the await() function. |
It is not possible for the launch to work like async in any case or condition. | If you use async and do not wait for the result, it will work exactly the same as launch. |
Launch can be used at places if you don’t need the result from the method called. | Use async when you need the results from the multiple tasks that run in parallel. |
Example: It can be used at places involving tasks like update or changing color like fetch User And Save In Database. | Example: Imagine the condition, when we have to fetch two users’ data from the database by using two parallel network calls and then use them for computing some results based on their data. |
Similar Reads
Scopes in Kotlin Coroutines
Scope in Kotlin's coroutines can be defined as the restrictions within which the Kotlin coroutines are being executed. Scopes help to predict the lifecycle of the coroutines. Kotlin CoroutinesScope is a term well used in the referred for the interface used for defining the Scope(lifetime and context
6 min read
Kotlin Coroutines on Android
Asynchronous programming is very important and it's now a common part of modern application. It increases the amount of work that an app can execute by allowing tasks to execute in parallel. This makes sure that heavy tasks run in the background, keeping the UI thread responsive and improving the ov
5 min read
Jobs, Waiting, Cancellation in Kotlin Coroutines
Prerequisite: Kotlin Coroutines On AndroidDispatchers in Kotlin CoroutinesSuspend Function In Kotlin Coroutines In this article, the following topics will be discussed like what are the jobs in a coroutine, how to wait for the coroutine, and how to cancel the coroutine. Whenever a new coroutine is l
7 min read
withContext in Kotlin Coroutines
Prerequisite: Kotlin Coroutines on AndroidLaunch vs Async in Kotlin Coroutines It is known that async and launch are the two ways to start the coroutine. Since It is known that async is used to get the result back, & should be used only when we need the parallel execution, whereas the launch is
3 min read
Suspend Function In Kotlin Coroutines
Prerequisite: Kotlin Coroutines on Android The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can execute. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. Kotlin coroutines introduce a
4 min read
Retrofit with Kotlin Coroutine in Android
Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is popular among the developers to use the API key. The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can e
3 min read
runBlocking in Kotlin Coroutines with Example
Prerequisite: Kotlin Coroutines on AndroidSuspend Function In Kotlin Coroutines As it is known that when the user calls the delay() function in any coroutine, it will not block the thread in which it is running, while the delay() function is called one can do some other operations like updating UI a
5 min read
Room Database with Kotlin Coroutines in Android
This project will be used for the implementation phase. If you have not yet completed the project, you should do so and then return. To keep things simple, the project employs a basic MVVM architecture. The complete code for the implementation mentioned in this blog can be found in the project itsel
3 min read
Dispatchers in Kotlin Coroutines
Prerequisite: Kotlin Coroutines on Android It is known that coroutines are always started in a specific context, and that context describes in which threads the coroutine will be started in. In general, we can start the coroutine using GlobalScope without passing any parameters to it, this is done w
3 min read
CountDownTimer in Android using Kotlin
CountDownTimer app is about setting a time that moves in reverse order as it shows the time left in the upcoming event. A CountDownTimer is an accurate timer that can be used for a website or blog to display the count down to any special event, such as a birthday or anniversary. Likewise, here letâs
2 min read