coroutineScope vs supervisorScope
Tags: android, kotlin
Published: 2022-10-05 05:58:26

---
title: coroutineScope vs supervisorScope
published: true
description: In this blog, we will learn about the coroutineScope vs supervisorScope of Kotlin Coroutines.
tags: android, kotlin
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1kye7rctczotpedgjw0.png
canonical_url: https://amitshekhar.me/blog/coroutinescope-vs-supervisorscope
---

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

In this blog, we will learn about the `coroutineScope` vs `supervisorScope` of Kotlin Coroutines.

There is a major difference between the `coroutineScope` and the `supervisorScope`, and we must understand that difference otherwise we will make mistakes without knowing what is happening with the Kotlin Coroutines code.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/coroutinescope-vs-supervisorscope).**

As always, I am taking a real example use-case to understand this major difference between the `coroutineScope` and the `supervisorScope`.

Example use case:

Suppose, we have two network calls as follows:

- `getUsers()`
- `getMoreUsers()`

We want to make these two network calls in parallel to get the data from the server.

You can find the complete source code of this example [here](https://github.com/amitshekhariitbhu/Learn-Kotlin-Coroutines).

In Kotlin Coroutines, we use `async` for doing tasks in parallel.

We can write our code as below:

```kotlin
launch {
    try {
        val usersDeferred = async {  getUsers() }
        val moreUsersDeferred = async { getMoreUsers() }
        val users = usersDeferred.await()
        val moreUsers = moreUsersDeferred.await()
    } catch (exception: Exception) {
        // handle exception
    }
}
```

Here, we will face one major problem, if any one of the two network calls leads to an error, the application will **crash!** it will **NOT** go to the catch block.

To solve this, we will have to use the `coroutineScope` as below:

```kotlin
launch {
    try {
        coroutineScope {
            val usersDeferred = async {  getUsers() }
            val moreUsersDeferred = async { getMoreUsers() }
            val users = usersDeferred.await()
            val moreUsers = moreUsersDeferred.await()
        }
    } catch (exception: Exception) {
        // handle exception
    }
}
```

Now, if any network error comes, it will go to the catch block. This is how `coroutineScope` helps.

But suppose again, we want to return an empty list for the network call which has failed and continue with the response from the other network call. We will have to use the `supervisorScope` and add the `try-catch` block to the individual network call as below:

```kotlin
launch {
    try {
        supervisorScope {
            val usersDeferred = async { getUsers() }
            val moreUsersDeferred = async { getMoreUsers() }
            val users = try {
                usersDeferred.await()
            } catch (e: Exception) {
                emptyList<User>()
            }
            val moreUsers = try {
                moreUsersDeferred.await()
            } catch (e: Exception) {
                emptyList<User>()
            }
        }
    } catch (exception: Exception) {
        // handle exception
    }
}
```

So now, if any error comes, it will continue with the empty list. This is how `supervisorScope` helps.

**The major difference:**

- **A `coroutineScope` will cancel whenever any of its children fail.**
- **A `supervisorScope` won't cancel other children when one of them fails.**

**Note:**

- Use `coroutineScope` with the top-level `try-catch`, when you do **NOT** want to continue with other tasks if any of them have failed.
- If we want to continue with the other tasks even when one fails, we go with the `supervisorScope`.
- Use `supervisorScope` with the individual `try-catch` for each task in addition to the top-level `try-catch`, when you want to continue with other tasks if one or some of them have failed.

This was all about the `coroutineScope` and the `supervisorScope` that are present in the Kotlin Coroutines.

That's it for now.

Thanks

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

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
- [Facebook](https://www.facebook.com/amit.shekhar.iitbhu)

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