5 Beyond callbacks

 

This chapter covers

  • Callbacks and their limitations, as shown with a gateway/edge service example
  • Futures and promises--a simple model to chain asynchronous operations
  • Reactive extensions--a more powerful model that is especially well suited to composing streams of asynchronous events
  • Kotlin coroutines--language-level support for asynchronous code execution flows

You will need to write all sorts of business logic while developing a reactive application, and not all logic is easy to express in an asynchronous form. While callbacks are a simple form of asynchronous event notification, they can easily render asynchronous code complicated.

Let’s look at a real example of why callbacks are not always the best asynchronous programming model. Then we’ll explore the multiple options supported in Vert.x.

5.1 Composing asynchronous operations: The edge service example

We’ll take the example of an edge service to illustrate composing asynchronous operations with different asynchronous programming models.

An edge service is also frequently called an API gateway. It is a service that serves as a facade for other services, so that a requester just has to deal with one service interface rather than having to talk to each service. An edge service may also perform other tasks, such as data transformation and interacting with other services, so it does not just conveniently aggregate data from multiple services.

5.1.1 Scenario

5.1.2 Heat sensor verticles

5.1.3 Snapshot service verticle

5.2 Callbacks

5.2.1 Implementation

5.2.2 Running

5.2.3 The “callback hell” is not the problem

5.3 Futures and promises

5.3.1 Futures and promises in Vert.x

5.3.2 Future-based APIs in Vert.x 4

5.3.3 Interoperability with CompletionStage APIs

5.3.4 Collector service with Vert.x futures

Summary