Thanks a lot for this great article. May I suggest a small imporovement. You can go further with suspend keyword and change your definition of asyncTask to this:

suspend fun <T> asyncTask(function: () -> T): T {
return run(CommonPool) { function() }

The actual change is to replace async with run. Because of this change, the Deferred<T> result simplifies to T and see how it is easier and less error-prone to use now:

suspend fun getListOfThings() {
asyncTask { storageHelper.getListFromDb() }
.let { list ->

You don’t need to remember to use await every time you use asyncTask now. You have even more safety, especially for case where you don’t otherwise need the result of your async action.

Written by

Project Lead for the Kotlin Programming Language @JetBrains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store