20 Coroutines

Applications perform all kinds of functions, including connecting with outside resources. You may want your app to download data, query a database, or make a request to a web API. These are all useful operations, but they can require a considerable amount of time to complete. You do not want your users to be stuck waiting for an operation to finish before they can continue using your app.

Instead of requiring users to wait while long-running work completes, you should move that work to execute in the background. If you do not, you block your program from reacting to any other events, making it look like your program has frozen. Coroutines let you define work that will run in the background – or, as it is often called, ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition 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.