# A dive into Async-Await on Android

DevFeed: [A dive into Async-Await on Android](<https://devfeed.tech/articles/a-dive-into-async-await-on-android-25997.md>)

Original publisher: [Read original article](<https://medium.com/@nhaarman/a-dive-into-async-await-on-android-5a6699029aa3?source=rss-fceb7a60a849------2>)

Author: Niek Haarman

Published: 2016-11-03T16:09:29Z

Content type: tutorial

Language: en

Sources: [Stories by Niek Haarman on Medium](<https://devfeed.tech/sources/stories-by-niek-haarman-on-medium.md>)

Topics: [async/await](<https://devfeed.tech/topics/async-await.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Code](<https://devfeed.tech/topics/code.md>), [ui](<https://devfeed.tech/topics/ui.md>), [Network](<https://devfeed.tech/topics/network.md>), [Database](<https://devfeed.tech/topics/database.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [async](<https://devfeed.tech/tags/async.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [await](<https://devfeed.tech/tags/await.md>), [blocking](<https://devfeed.tech/tags/blocking.md>), [callback](<https://devfeed.tech/tags/callback.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [database](<https://devfeed.tech/tags/database.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-compiler](<https://devfeed.tech/tags/kotlin-compiler.md>), [main-thread](<https://devfeed.tech/tags/main-thread.md>), [network](<https://devfeed.tech/tags/network.md>), [textview](<https://devfeed.tech/tags/textview.md>)

## AI overview

This tutorial explains async-await on Android using Kotlin coroutines, focusing on handling long-running network and database operations without blocking the UI thread. It contrasts nested callbacks with suspension functions such as asyncUI and await, and describes how the Kotlin compiler transforms the code.

## Source excerpt

Note: this article was written for a preview version of coroutines for Kotlin. Its details have changed since then. In a previous article I provided a glimpse into the world of async-await on Android. Now it's time to dive a little bit deeper in this upcoming functionality in Kotlin 1.1 What is async-await for? When dealing with long-running operations like network calls or database transactions, you need to make sure you schedule this work to a background thread. If you forget to do this, you may end up with blocking the UI thread until the task is finished. During that time, the user cannot interact with your application. Unfortunately when you schedule a new task in the background, you cannot use its result directly. Instead, you're gonna have to use some sort of callback. When that callback is invoked with the result of the operation, you can continue with what you want to do, for example run another network request. This easily flows in what people call a 'callback hell': multiple nested callbacks, all waiting to be invoked when some long-running task has finished. fun retrieveIssues() { githubApi.retrieveUser() { user -> githubApi.repositoriesFor(user) { repositories -> githubApi.issueFor(repositories.first()) { issues -> handler.post { textView.text = "You have issues!" } } } } } This snippet of code does three network requests, and finally posts a message to the main thread to update the text of some TextView. Fixing this with async-await With async-await, you can program that same function in a more imperative way. Instead of passing a callback to the function, you can call a suspension function await which lets you use the result of the task in a way that resembles normal synchronous code: fun retrieveIssues() = asyncUI { val user = await(githubApi.retrieveUser()) val repositories = await(githubApi.repositoriesFor(user)) val issues = await(githubApi.issueFor(repositories.first())) textView.text = "You have issues!" } This snippet of code still does three n