Using coroutines in real life

Microbenchmarks are very funny and they give us an idea of the power of Kotlin coroutines, but they don't represent a real-case scenario.

Let's introduce our real-case scenario:

enum class Gender {   MALE, FEMALE;   companion object {      fun valueOfIgnoreCase(name: String): Gender = valueOf(name.toUpperCase())   }}typealias UserId = Intdata class User(val id: UserId, val firstName: String, val lastName: String, val gender: Gender)data class Fact(val id: Int, val value: String, val user: User? = null)interface UserService {   fun getFact(id: UserId): Fact}

Our UserService interface has just one method—getFact will return a Chuck Norris-style fact about our user, identified by the user ID.

The implementation should check ...

Get Functional 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.