Skip to content
Ameen Abozaid
Planned lessonMID-006-L02

Composing stages

  • Lesson 2 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

pick the right combinator.

Planned topics

  • thenApply(fn): transform the result (map).
  • thenCompose(fn): fn returns another future; flattens (flatMap). Use for dependent calls.
  • thenCombine(other, fn): join two independent futures.
  • allOf(...): completes when all do; it returns Void, so read each future afterwards. anyOf takes the first.
  • thenAccept/thenRun for side effects at the end.
Draft outline details

Demonstration

java
var page = product.thenCombine(stock, PageParts::new)
                  .thenCombine(reviews, (parts, r) -> parts.with(r));
CompletableFuture<Order> o = userF.thenCompose(u -> loadLastOrder(u.id()));

Check your understanding

Which combinator for "call B using A's result, where B is async"?

Show answer

thenCompose.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.