Asynchronous Programming Techniques with Kotlin – hackernoon.com
Author profile picture

An introduction to Kotlin coroutines compared to Futures/Promises such as Reactor’s Mono or Java’s CompletableFuture

Want to jump right into code? Go check it here.

Introduction

While reading about Kotlin’s coroutines, I thought it would be really helpful
to compare it to other async programming techniques, specifically the
Futures/Promises approach that is widely used in Java.

Even though the Kotlin documentation is pretty good and comprehensive, I felt the lack of a more “real world” example. This is an attempt to fill that gap.

Do not expect to have a comprehensive view of how coroutines work nor how Java’s

CompletableFuture

or Reactor’s

Mono

work.

The idea is to pick a quite simple made-up problem, implement it
with the different approaches and libraries, and compare them. This is
an introductory material to the topic.

Now let’s get into the made-up problem.

Problem Statement

Who never needed to work on a problem where you need to get data from one place, do some processing and send it to a different place? The made-up problem here is basically that.

Assume we work on e-commerce and we want to get the most recent order id from an HTTP endpoint, send this id to two other endpoints that:

  • Give us the stock information about the items in the order, and;
  • Give us the calculated delivery cost for the order.

Then we
Source…