What are coroutines?

Coroutines are components of a computer program that can be considered lightweight threads. Coroutines allow us to suspend the invocation of a function without blocking a thread. Let's imagine a case in which you need to perform a request on the server and display a progress bar until your app receives the response. A request is a long-term operation that should be performed asynchronously because a user interface should stay responsive. This is a common approach to running a new thread that uses a callback so that you're notified when the app receives a response. However, using code with callbacks looks unnatural, complex, and can lead to bugs.

Coroutines can be considered as a library that wraps a particular part of ...

Get Hands-On Object-Oriented Programming with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.