Kotlin Coroutines Wait Task to Complete

runBlocking or async await

Assuming you launch a coroutines on main/ui thread to display result from an heavy io operation.

launch(Dispatchers.Main) {    val result = runBlocking(Dispatchers.IO) {        fetchSomething()    }    Toast.makeText(context, result, Toast.LENGTH_LONG).show()}

or

launch(Dispatchers.Main) {    val queue = async(Dispatchers.IO) {        fetchSomething()    }    val result = queue.await()    Toast.makeText(context, result, Toast.LENGTH_LONG).show()}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.