Search⌘ K
AI Features

async and await

Explore how to use async and await in Kotlin coroutines to execute tasks asynchronously and retrieve results. Understand how these functions differ from launch, how they handle concurrency, and how they propagate exceptions, helping you manage asynchronous code effectively.

We'll cover the following...

Why async & await are used?

The launch() function returns a Job object that can be used to await termination of the coroutine or to cancel. But there’s no way to return a result from the coroutine that was started using launch(). If you want to execute a task asynchronously and get the response, then use async() instead of launch().

The async() function takes the same parameters as launch(), so we can configure the context and start properties of coroutines that are created using async() ...