Structured concurrency

Asynchronous operations

fun requestSomeData() {
launch(UI) {
updateUI(performRequest())
}
}
fun requestSomeData() {
launch {
updateUI(performRequest())
}
}

Parallel decomposition

suspend fun loadAndCombine(name1: String, name2: String): Image { 
val deferred1 = async { loadImage(name1) }
val
deferred2 = async { loadImage(name2) }
return
combineImages(deferred1.await(), deferred2.await())
}
suspend fun loadAndCombine(name1: String, name2: String): Image =
coroutineScope {
val deferred1 = async { loadImage(name1) }
val
deferred2 = async { loadImage(name2) }
combineImages(deferred1.await(), deferred2.await())
}

Further reading

--

--

--

Project Lead for the Kotlin Programming Language @JetBrains

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Change MAC Address on Linux

Top new features of the new Flutter 3

How to Migrate Data from an ECS-hosted User-created Elasticsearch Cluster to an Alibaba Cloud…

How to Install and Secure Mosquitto MQTT Messaging Broker on Ubuntu 16.04

‘Serverless’ — Is this Computing World Reliable?

Some quotes about small steps

Retrospection 2021: Top Labs, Quests and more!

Running Jupyter Notebooks on a Ubuntu Server

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
Roman Elizarov

Roman Elizarov

Project Lead for the Kotlin Programming Language @JetBrains

More from Medium

Introduction to Kotlin Coroutines

Leetcode — Kth Smallest Element in a BST (Kotlin)

Avoiding Stringly-typed in Kotlin

Kotlin Multiplatform library with Kotest and Gradle Version Catalog