Using Kotlin Coroutines with Retrofit

Introduction

We can use Kotlin coroutines to write asynchronous code in Android applications. The previous lesson outlined the noteworthy features of coroutines and discussed a few examples.

In this lesson, we’ll learn to use Kotlin coroutines with Retrofit. Earlier, we saw a simple usage of Retrofit with enqueue and callbacks. Instead of using enqueue and callbacks, we’ll use coroutines’ `suspend` feature for asynchronous execution.

Defining the data class

We’ll use an open-source API to fetch a list of countries. Therefore, our data class will stay the same.

Get hands-on with 1200+ tech skills courses.