# A glimpse of Async-Await on Android

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

Original publisher: [Read original article](<https://medium.com/@nhaarman/async-await-in-android-f0202cf31088?source=rss-fceb7a60a849------2>)

Author: Niek Haarman

Published: 2016-10-31T12:25:13Z

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>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Android](<https://devfeed.tech/topics/android.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [API](<https://devfeed.tech/topics/api.md>), [GitHub API](<https://devfeed.tech/topics/github-api.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>), [await](<https://devfeed.tech/tags/await.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [database](<https://devfeed.tech/tags/database.md>), [developer](<https://devfeed.tech/tags/developer.md>), [github](<https://devfeed.tech/tags/github.md>), [i](<https://devfeed.tech/tags/i.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [network](<https://devfeed.tech/tags/network.md>), [techniques](<https://devfeed.tech/tags/techniques.md>), [thread](<https://devfeed.tech/tags/thread.md>), [threads](<https://devfeed.tech/tags/threads.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This Android developer article compares several ways to perform network and I/O work without blocking the main thread, including threads, AsyncTask, callbacks, Rx, and Kotlin coroutines with async-await. Using a GitHub API example, it shows how coroutines can suspend computation while allowing continuation on the main thread, improving code readability. The article notes that it targets a preview version of Kotlin coroutines and omits cancellation and listener-removal details.

## Source excerpt

Note: this article was written for a preview version of coroutines for Kotlin. Its details have changed since then. Kotlin 1.1 will bring coroutines to the language, which allows computations to be suspended at some points and continue later on. The obvious example is async-await, as introduced a couple of years ago in C#. Every Android developer knows that when you deal with network requests and other I/O tasks, you will need to make sure you don't block the main thread, and don't touch the UI from a background thread. Over the years dozens of techniques have come and gone. This article lists a few of the most used ones, and shows an example of the goodness that async-await can bring. The scenario We will fetch a user instance from the Github api and store it in some database. When this is done we show the result on-screen. I won't be explaining the techniques, as they should speak for themselves. Plain old threads Manual, full control fun threads() { val handler = Handler() Thread { try { val user = githubApi.user() userRepository.store(user) handler.post { threadsTV.text = "threads: [$user]" } } catch(e: IOException) { handler.post { threadsTV.text = "threads: [User retrieval failed.]" } } }.start() }Android's AsyncTask Nobody uses these anymore, right? fun asyncTask() { object : AsyncTask<Unit, Unit, GithubUser?>() { private var exception: IOException? = null override fun doInBackground(vararg params: Unit): GithubUser? { try { val user = githubApi.user() userRepository.store(user) return user } catch(e: IOException) { exception = e return null } } override fun onPostExecute(user: GithubUser?) { if (user != null) { asyncTaskTV.text = "asyncTask: [$user]" } else { asyncTaskTV.text = "asyncTask: [User retrieval failed.]" } } }.execute() }Callbacks Callback-hell, anyone? fun callbacks() { githubApi.userFromCall().enqueue(object : Callback<GithubUser> { override fun onResponse(call: Call<GithubUser>, response: Response<GithubUser>) { val user = response.body() userR