# 5.5.1 Coroutines

If you were to run a networking request on its own, there can be many things that goes wrong. Perhaps the networking request takes a long time to complete, and the user is left waiting. Perhaps the networking request fails, but the application has no way to detect and handle the failure. This is where coroutines come into play.&#x20;

## Blocking vs Suspending Functions

One of the most important characteristics of coroutines is that it supports suspending functions. Normally when a networking call is made out without some sort of wrapper, it is a blocking call where it blocks all other operations on the thread unless the call is completed. This is not ideal as we don't want to leave the user hanging.&#x20;

In the example below, Function A and Function B are blocking calls where Function B cannot run until Function A has been completed.&#x20;

![Blocking function diagram](/files/-ML9PPtK6HquWSn9GgQZ)

However, with coroutines, we can build suspended calls where several functions can run concurrently. In the example below, even if Function A is started first, it will suspend itself to allow Function B to run and finish before unsuspending and finishing. The great thing about coroutines is that it'll figure out the order of suspending functions to optimize the runtime for developers! &#x20;

![Suspending function diagram](/files/-ML9PVQVDhgFtQxbJTGU)

Figure 1 and 2 [source](https://medium.com/mobile-app-development-publication/understanding-suspend-function-of-coroutines-de26b070c5ed#:~:text=BLOCKING%3A%20Function%20A%20has%20to,not%20locked%20by%20Function%20A.)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://android-course.cornellappdev.com/chapters/5.5-concurrency/7.2-coroutines.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
