← Lessons

quiz vs the machine

Gold1430

Concurrency

The Completable Future Composition

Chaining and combining asynchronous results.

5 min read · core · beat Gold to climb

Beyond a single result

A composable future lets you build pipelines without blocking. You describe what to do with a result before it exists, and the runtime wires the steps together.

The core combinators

  • Then apply transforms a result into a new value.
  • Then compose chains a result into another future, flattening nested futures.
  • Combine waits for two independent futures and merges their results.
  • Exceptionally supplies a fallback when a stage fails.

The distinction between apply and compose mirrors map versus flat map: apply returns a plain value, compose returns another future and avoids a future of a future.

Error propagation

A failure in any stage short circuits the rest of the chain and travels to the nearest handler. This means you do not check errors at every step; you place one recovery stage where it belongs.

Fan out and fan in

You can launch several independent futures, then join them with an all of combinator that completes when every input completes. That is the natural shape for parallel requests.

Key idea

Composable futures turn nested callbacks into flat pipelines using apply, compose, combine, and a single error handler.

Check yourself

Answer to earn rating on the learn ladder.

1. What distinguishes then compose from then apply?

2. How do errors travel through a future chain?