Image for post
Image for post

Swap callback hell for sexy suspend!

Available in: English, French, Thai

Kotlin coroutines provide a concise & elegant syntax for running concurrent & async tasks. Unlike callbacks, suspend funs can be wrapped & chained.

…but my legacy component only provides a callback interface

Android’s & dialogs are such a case! We just want a value from the user, but we cannot return that value directly, since the picker is launched asynchronously. So we’re stuck with ugly callbacks:

Traditional callback code (ugly)


Fortunately, Kotlin coroutines provide an extension precisely for this scenario: . …

Charles Allen

Also available in Thai:

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