Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Kotlin Standard Library Cookbook

You're reading from   Kotlin Standard Library Cookbook Master the powerful Kotlin standard library through practical code examples

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781788837668
Length 242 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Urbanowicz Urbanowicz
Author Profile Icon Urbanowicz
Urbanowicz
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
1. Ranges, Progressions, and Sequences FREE CHAPTER 2. Expressive Functions and Adjustable Interfaces 3. Shaping Code with Kotlin Functional Programming Features 4. Powerful Data Processing 5. Tasteful Design Patterns Adopting Kotlin Concepts 6. Friendly I/O Operations 7. Making Asynchronous Programming Great Again 8. Best Practices for the Android, JUnit, and JVM UI Frameworks 9. Miscellaneous 1. Other Books You May Enjoy Index

Wrapping third-party callback-style APIs with coroutines


Often third-party libraries offer callback-style asynchronous APIs. However, the callback functions are considered to be an anti-pattern, especially whenever we are dealing with a number of nested callbacks. In this recipe, we are going to learn how to deal with libraries that provide callback-style methods by transforming them easily into suspending functions that can be run using coroutines.

Getting ready

The first step to start working with Kotlin Coroutines is to add the core framework dependency to the project:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.3'

The preceding code declares the kotlinx-coroutines-core dependency in a Gradle build script, which is used in the sample project (https://github.com/PacktPublishing/Kotlin-Standard-Library-Cookbook). 

As far as the recipe task is concerned, let's assume we have a class called Result, defined as follows:

data class Result(val displayName: String)

Here is the...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images