← Lessons

quiz vs the machine

Gold1410

Concurrency

Futures and Promises Functional

Compose asynchronous results without ever blocking a thread.

5 min read · core · beat Gold to climb

A result that arrives later

A future is a value that is not ready yet. In a functional style you never block waiting for it. Instead you transform the future itself, describing what to do once the value arrives.

Map and flatMap

  • map applies a pure function to the eventual value, producing a new future.
  • flatMap chains a step that itself returns a future, flattening two async stages into one.

Because these return new futures, you build a pipeline of computation that the runtime executes as results settle, without any thread sitting idle on a wait call.

Combining many

You can combine independent futures: start several at once and join them when all complete. Since each runs on its own and you only describe the combination, the work overlaps naturally.

Errors as values

A functional future carries either a success or a failure. A failure short circuits the chain: later map steps are skipped and the error flows to a recovery handler. This keeps error handling in the same composable pipeline rather than scattered try blocks.

Key idea

Functional futures are composed with map and flatMap so async results chain into pipelines that never block a thread.

Check yourself

Answer to earn rating on the learn ladder.

1. What does flatMap on a future do that map does not?

2. How does a failure behave in a functional future chain?