Launch vs Async in Kotlin Coroutines
Tags: kotlin, android
Published: 2023-02-16 10:42:17

---
title: Launch vs Async in Kotlin Coroutines
published: true
description: In this blog, we will learn about the Launch vs Async in Kotlin Coroutines.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7mq4eftv7e6phqhts7c.png
canonical_url: https://amitshekhar.me/blog/launch-vs-async-in-kotlin-coroutines
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out [Android Interview Questions and Answers](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

In this blog, we will learn about the Launch vs Async in Kotlin Coroutines. We will see how the Launch and Async differ from each other and when to use which one.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/launch-vs-async-in-kotlin-coroutines).**

Let's get started.

Both launch and async are the functions in Kotlin to start the Coroutines.

- `launch{}`
- `async{}`

The difference is that the `launch{}` returns a `Job` and does not carry any resulting value whereas the `async{}` returns an instance of `Deferred<T>`, which has an `await()` function that returns the result of the coroutine like we have future in Java in which we do `future.get()` to get the result.

In other words:

- **launch**: fire and forget.
- **async**: perform a task and return a result.

Let's take an example to learn launch vs async.

We can use the `launch` as below:

```kotlin
val job = GlobalScope.launch(Dispatchers.Default) {
    // do something and do not return result
}
```

It returns a job object which we can use to get a job's status or to cancel it.

In the above example of launch, we have to do something and **NOT** return the result back.

But when we need the result back, we need to use the `async`.

```kotlin
val deferredJob = GlobalScope.async(Dispatchers.Default) {
    // do something and return result, for example 10 as a result
    return@async 10
}
val result = deferredJob.await() // result = 10
```

Here, we get the result using the `await()`.

In async also, we can use the `Deferred` job object to get a job's status or to cancel it.

Note: I have used GlobalScope for quick examples, we should avoid using it at all costs. In an Android project, we should use custom scopes based on our usecase such as `lifecycleScope`, `viewModelScope` and etc.

**Another difference between launch and async is in terms of exception handling.**

If any exception comes inside the **launch** block, it crashes the application if we have not handled it.

However, if any exception comes inside the async block, it is stored inside the resulting `Deferred` and is not delivered anywhere else, it will get silently dropped unless we handle it.

Let's understand this difference with code examples.

Suppose we have a function that does something and throws an exception:

```kotlin
private fun doSomethingAndThrowException() {
    throw Exception("Some Exception")
}
```

Now using it with the **launch**:

```kotlin
GlobalScope.launch {
    doSomethingAndThrowException()
}
```

It will **CRASH** the application as expected.

We can handle it as below:

```kotlin
GlobalScope.launch {
    try {
        doSomethingAndThrowException()
    } catch (e: Exception) {
        // handle exception
    }
}
```

Now, the exception will come inside the catch block and we can handle it.

And now, using it with the `async`:

```kotlin
GlobalScope.async {
    doSomethingAndThrowException()
}
```

The application will **NOT** crash. The exception will get dropped silently.

Again, we can handle it as below:

```kotlin
GlobalScope.async {
    try {
        doSomethingAndThrowException()
    } catch (e: Exception) {
        // handle exception
    }
}
```

Now, the exception will come inside the catch block and we can handle it.

Let me tabulate the difference between launch and async.

| Launch                                                                                                | Async                                                                                                                                                                           |
| :---------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Fire and forget.                                                                                      | Perform a task and return a result.                                                                                                                                             |
| `launch{}` returns a `Job` and does not carry any resulting value.                                    | `async{}` returns an instance of `Deferred<T>`, which has an `await()` function that returns the result of the coroutine.                                                       |
| If any exception comes inside the launch block, it crashes the application if we have not handled it. | If any exception comes inside the async block, it is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless we handle it. |

So, we have understood the difference between the launch function and the async function.

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)